Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > __eq__ and __ne__

Reply
Thread Tools

__eq__ and __ne__

 
 
Shane Hathaway
Guest
Posts: n/a
 
      07-08-2003
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.

>>> class foo:

.... def __eq__(self, other):
.... return (other.__class__ is self.__class__
.... and other.__dict__ == self.__dict__)
....
>>> foo() == foo()

1
>>> foo() != foo()

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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Instances as dictionary key, __hash__ and __eq__ Tim Delaney Python 0 02-18-2013 09:16 PM
Is __ne__ method autogenerated? INADA Naoki Python 3 12-07-2012 06:48 AM
hash and __eq__ Aaron Brady Python 14 06-04-2009 01:41 PM
RE: __eq__ and __ne__ Tim Peters Python 0 07-16-2003 02:14 PM
RE: __eq__ and __ne__ Tim Peters Python 0 07-08-2003 03:54 PM



Advertisments