In the python docs (yeah, I have this thing with the docs) it says that:
User-defined classes have
__cmp__()and__hash__()methods by default; with them, all objects compare unequal (except with themselves) andx.__hash__()returnsid(x).
But the following code shows another thing:
>>> 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__'
>>>
So where is __cmp__ or what am I missing?
The documentation is a bit misleading. To get the full story, you have to read up on
__cmp__, namely this part:So, basically, you don’t get a
__cmp__method by default, but instances of user-defined classes can be compared to other objects; if the instance has no__cmp__method, the object identity (determined byid(obj), which is usually the memory address of the object) will be used instead.