Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

RE: __eq__ and __ne__

 
 
Tim Peters
Guest
Posts: n/a
 
      07-08-2003
[Shane Hathaway]
> I was surprised by the following behavior. Apparently, the "!="
> operator does not fall back to using "not __eq__()".


That's true -- there are no magical equivalences among the six rich
comparison operators.

> I tested this with Python 2.1, 2.2, and 2.2 with new-style classes and
> got the same results in every case.


Whew! I was afraid a bug may have slipped in <wink>.

> >>> 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)


That's appropriate if that's what you what "!=" to mean.

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


The language (not library) Ref Man says this (under "Basic Customization"),
and means what it says:

The correspondence between operator symbols and method names is as
follows: x<y calls x.__lt__(y), x<=y calls x.__le__(y), x==y
calls x.__eq__(y), x!=y and x<>y call x.__ne__(y), x>y calls
x.__gt__(y), and x>=y calls x.__ge__(y).

In the richcmp PEP:

http://www.python.org/peps/pep-0207.html

point 3 under "Proposed Resolutions" gives one rationale:

3 The == and != operators are not assumed to be each other's
complement (e.g. IEEE 754 floating point numbers do not satisfy
this). It is up to the type to implement this if desired.
Similar for < and >=, or > and <=; there are lots of examples
where these assumptions aren't true (e.g. tabnanny).

A more involved (and probably more basic) rationale has to do with that the
richcmp operators aren't constrained to return Boolean results; as the PEP
says at its start:

The main motivation comes from NumPy, whose users agree that A<B
should return an array of elementwise comparison outcomes;

Since the richcmp operators aren't constrained to return scalars, an attempt
to define one in terms of the others seemed futile. Instead they're 6
independent methods.


 
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
__eq__ and __ne__ Shane Hathaway Python 0 07-08-2003 03:15 PM



Advertisments