下面這段代碼工作在Python 3(3.5.2)罰款,但提出了在Python 2(2.7.12)__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的行爲是正常的AttributeError: 'super' object has no attribute '__eq__'
。我試圖理解爲什麼它不會在Python 2
工作,請注意,這個問題不是'super' object has no attribute '__eq__'
因爲在Python 2'int'不具有比較豐富的操作符(見[這裏](http://stackoverflow.com/questions/10809932/python-why-does-the-int-class-not-have-rich -comparison運營商狀-LT))。 Python 3實現了豐富的比較運算符,因爲'__cmp__'被解釋了。 –