__eq__ and __ne__

S

Shane Hathaway

I was surprised by the following behavior. Apparently, the "!="
operator does not fall back to using "not __eq__()". I tested this with
Python 2.1, 2.2, and 2.2 with new-style classes and got the same results
in every case.
.... def __eq__(self, other):
.... return (other.__class__ is self.__class__
.... and other.__dict__ == self.__dict__)
....1

I would expect the second test to yield "0". To compensate, I've
started adding the following boilerplate code to all classes that define
only __eq__:

def __ne__(self, other):
return not self.__eq__(other)

Does this surprise anyone else? I have not yet found documentation on this.

Shane
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top