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:
If no
__cmp__(),__eq__()or__ne__()operation is defined, class instances are compared by object identity (“address”).
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 by id(obj), which is usually the memory address of the object) will be used instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With