【发布时间】:2015-09-10 13:05:37
【问题描述】:
在此示例中,我尝试分配新的__eq__、__lt__ 和__gt__ 方法,并通过将对象与文字3 进行比较来调用它们。为什么从未调用过bar(...)?
class Foo:
def __init__(self):
self.__eq__ = self.bar
self.__lt__ = self.bar
self.__gt__ = self.bar
def bar(self, other):
print("bar called.")
a = Foo()
a == 3
a < 3
a > 3
在this answer 中我找到了提示you can't assign new __eq__ to the object。不过,我找不到该声明的任何官方来源。
感谢任何提示和帮助!
编辑:我知道在对象本身中有定义__eq__ 的选项,但这个问题专门关于在实例创建后分配它。
【问题讨论】:
标签: python python-3.x