13
在Python docs(yeah, I have this thing with the docs)它說:Python - 用戶定義的類默認具有__cmp __()和__hash __()方法?要麼?
用戶定義的類具有
__cmp__()
和__hash__()
方法默認;與他們,所有的對象比較不平等(除了與他們自己)和x.__hash__()
返回id(x)
。
但下面的代碼顯示了另一件事:
>>> class Test(object): pass
...
>>> t = Test()
>>>
>>> t.__hash__
<method-wrapper '__hash__' of Test object at 0x01F2B5D0>
>>>
>>> t.__cmp__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Test' object has no attribute '__cmp__'
>>>
那麼,是__cmp__
還是我缺少什麼?