【发布时间】:2012-10-05 21:34:30
【问题描述】:
在 python docs (yeah, I have this thing with the docs) 中它说:
用户自定义类默认有
__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__ 在哪里或者我错过了什么?
【问题讨论】:
标签: python methods comparison default