Annoying behaviour of the != operator

Dave Benjamin ramen at lackingtalent.com
Wed Jun 8 13:32:22 EDT 2005


Jordan Rastrick wrote:
> Surely the != operator should, if no __ne__ method is present for
> either object, check to see if an __eq__ method is defined, and if so,
> return its negation?
> 
> Actually, that brings me to a wider question - why does __ne__ exist at
> all? Surely its completely inconsistent and unnessecary to have
> seperate equals and not equals methods on an object? a != b should just
> be a short way of writing not (a == b). The fact the two can give a
> different answer seems to me to be utterly unintuitive and a massive
> pitfall for beginners (such as myself).

I agree that it's confusing. I didn't even understand this behavior 
myself until I went and looked it up:

http://docs.python.org/ref/customization.html

The rationale for this behavior is in PEP 207 -- Rich Comparisons:

http://python.fyxm.net/peps/pep-0207.html

Personally, I implement the __cmp__ method when I want my objects to be 
comparable, so I've never run into this problem.

Dave



More information about the Python-list mailing list