Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do the Python docs say I need to define __ne__ when I define __eq__?

According to the Python docs: "when defining __eq__(), one should also define __ne__() so that the operators will behave as expected".

However, it appears that Python computes __ne__ as not __eq__ automatically:

In [8]: class Test:
    def __eq__(self, other):
        print("calling __eq__")
   ...:         return isinstance(other, Test)
   ...:

In [9]: a = Test()

In [10]: b = Test()

In [11]: a == b
calling __eq__
Out[11]: True

In [12]: a != b
calling __eq__
Out[12]: False

In [13]: a == 1
calling __eq__
Out[13]: False

In [14]: a != 1
calling __eq__
Out[14]: True

So what's the point of defining __ne__ if it's just going to be return not self.__eq__(other)? And furthermore, where is this behavior actually documented?

EDIT

Apparently it matters that I am using Python 3. In Python 2, I get

In [1]: class Test(object):
   ...:     def __eq__(self, other):
   ...:         print("calling __eq__")
   ...:         return isinstance(other, Test)
   ...:

In [2]: a = Test()

In [3]: b = Test()

In [4]: a == b
calling __eq__
Out[4]: True

In [5]: a != b
Out[5]: True

In [6]: a == 1
calling __eq__
Out[6]: False

In [7]: a != 1
Out[7]: True

But the docs I referenced are the Python 3 docs. Were they just not updated?

like image 597
asmeurer Avatar asked Jun 27 '14 15:06

asmeurer


People also ask

What is the __ eq __ method in Python?

Python automatically calls the __eq__ method of a class when you use the == operator to compare the instances of the class.

What is Python __ add __?

Python's object. __add__(self, other) method returns a new object that represents the sum of two objects. It implements the addition operator + in Python. We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”).

What is __ GE __ in Python?

__ge__(y) to obtain a return value when comparing two objects using x >= y . The return value can be any data type because any value can automatically converted to a Boolean by using the bool() built-in function. If the __ge__() method is not defined, Python will raise a TypeError .

What is the IN operator in Python?

The 'in' Operator in Python The in operator works with iterable types, such as lists or strings, in Python. It is used to check if an element is found in the iterable. The in operator returns True if an element is found. It returns False if not.


1 Answers

Python 3 changed behaviour for the == case, see Python 3, What's New:

!= now returns the opposite of ==, unless == returns NotImplemented.

It was deemed a useful change.

The fact that the documentation has not been updated is indeed a long standing bug.

However, as a comment on the report points out, if you inherit from a class that already has defined __ne__, overriding just __eq__ is not enough and you'll also have to override the __ne__ method.

like image 80
Martijn Pieters Avatar answered Sep 24 '22 17:09

Martijn Pieters