【发布时间】:2016-11-28 16:54:35
【问题描述】:
以下代码在 Python 3 (3.5.2) 中运行良好,但在 Python 2 (2.7.12) 中引发 AttributeError: 'super' object has no attribute '__eq__'
class Derived(int):
def __eq__(self, other):
return super(Derived, self).__eq__(other)
a, b = Derived(1024), Derived(1729)
print(a == b)
Python 3 行为是预期的。我试图理解为什么它在 Python 2 中不起作用。
请注意,这个问题不是'super' object has no attribute '__eq__'的重复问题
【问题讨论】:
-
因为在 Python 2 中
int没有丰富的比较运算符(参见 here)。 Python 3 实现了丰富的比较运算符,因为__cmp__已被弃用。
标签: python python-2.7 super