cmp [__eq__]
Daniel Yoo
dyoo at hkn.eecs.berkeley.edu
Thu Dec 27 15:42:31 EST 2001
Mark McEahern <markjunk at mceahern.com> wrote:
:> Perhaps then could someone explain to me how one compares classes
:> useing the == operator, or indeed if this bad programming practice.
: By overloading the __cmp__ method. Don't confuse the return value of
: __cmp__ with whether or not the result of the comparison is that the two
: instances being compared are equal. Examine this sample and note that
: __cmp__ isn't merely used to determine equality. It is also used to
: determine sort order.
And if you just want to be able to compare two things for equality,
it's probably better to write an __eq__ method instead.
Unlike __cmp__, __eq__ is meant to be a boolean function, so it should
be more familiar to use. There's more information on __eq__ and the
other rich comparison methods here:
http://www.python.org/doc/current/ref/customization.html
Good luck!
More information about the Python-list
mailing list