Customisation of comparison operators

Tim Peters tim.one at comcast.net
Fri Mar 22 16:39:52 EST 2002


[Dave Haynes]
> Which of __lt__, __le__, __eq__, __ne__, __gt__, __ge__ are used when
> sorting lists of objects with <list>.sort()?

Only __lt__, provided it's defined.

> Which of them is used for the 'in' and 'not in' comparisons?

Impossible to say unless you first tell me the types of all objects
involved.  If you're talking about builtin lists, only __eq__, provided it's
defined.  The meaning of 'x in y' is, in general, determined by y's
__contains__ implementation (provided it's defined).

>  Is __cmp__ no longer used at all if the above are defined?

Impossible to say unless etc <wink>.

> This might be dodgy practice, but I have some objects for which I
> want to provide subtly different behaviour for the comparison
> operators than for testing 'in' and 'not in'...

Yup, that's dodgy all right.





More information about the Python-list mailing list