__eq__ and __ne__

Shane Hathaway shane at zope.com
Tue Jul 8 18:48:23 CEST 2003


Jeremy Hylton wrote:
> On Tue, 2003-07-08 at 12:08, Shane Hathaway wrote:
> 
>>Tim Peters wrote:
>>
>>>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.
>>
>>Ok.  Thanks for finding the docs.  I guess I'll stick to my boilerplate 
>>code.  I think some guidance should be added to the Python docs, though, 
>>explaining that whenever you define __eq__, you very likely ought to 
>>define __ne__ also.
> 
> I believe C++ has the same behavior when you overload logical
> operators.  So at least people familiar with the same behavior in other
> languages won't be surprised.

Actually, C++ generates a compiler error if you try to do that.  (See 
the attached program.)  Java doesn't run into this because it has a 
simpler interface: the only operator you can override is equals().  So 
in this case, C++ is fully explicit and Java is fully implicit, while 
Python makes a guess.  If we can't make it implicit like Java, then it 
seems preferable for Python to raise an exception, similar to C++, when 
you use the != operator with an instance that implements __eq__ but not 
__ne__.

Shane
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: test.cpp
URL: <http://mail.python.org/pipermail/python-list/attachments/20030708/f5648a39/attachment.ksh>


More information about the Python-list mailing list