当我在处理重载操作符和namedtuples时,我偶然发现了一些奇怪的行为,出于某种原因:
https://repl.it/repls/RemorsefulFlawlessAfricanwildcat
import collections, math
Point = collections.namedtuple("Point", ["x", "y"])
Point.__floor__ = lambda self: Point(int(math.floor(self.x)), int(math.floor(self.y)))
print(math.floor(Point(1.4, -5.9)))
#prints: Point(x=1, y=-6)
有人对此有什么见解吗?为什么有用?
如果我删除Point.__floor__
行,它就不起作用了。在
数学包是否在某处定义了__floor__
运算符?
Python是否解析Point.__XXX__
来提取{
我很困惑,可能是因为我不知道这些东西到底是怎么运作的。在
从文件(重点是我的):
相关问题 更多 >
编程相关推荐