Python interpreter bug

Carsten Haese carsten at uniqsys.com
Fri Oct 7 11:10:33 EDT 2005


On Fri, 2005-10-07 at 10:33, alainpoint at yahoo.fr wrote:
> In fact, i want to sort the list based on the 'allocated attribute' and
> at the same time, test membership based on the id attribute.
> __cmp__ logically implies an ordering test, not an identity test. These
> two notions seems to be confounded in python which is unfortunate. Two
> objects could have the same rank while still being different.
> Alain

You could use the id in __cmp__ as a tie-breaker if the two objects have
equal "allocated" values.

By the way, __cmp__ is not an identity test. The "in" operator doesn't
use object identity, it uses object equality, and __cmp__ does serve to
test object equality (if no __eq__ method is present).

Perhaps the following example will clarify the behavior of "in":

>>> A = [1]
>>> B = [1]
>>> A==B
True
>>> A is B
False
>>> A in ["spam", 42, B]
True

HTH,

Carsten Haese.





More information about the Python-list mailing list