Does __ne__ use an overridden __eq__?

Suppose I have the following program:

class A(object): def __eq__(self, other): return True a0 = A() a1 = A() print a0 != a1 

If you started it with Python, the output will be True . My question is:

  • The __ne__ method __ne__ not implemented, does Python work by default?
  • If Python uses the default method to determine whether two objects are equal or not, should it not call __eq__ and then deny the result?
+6
source share
1 answer

From documents :

There are no implied relationships between comparison operators. True x==y does not mean that x!=y is false. Accordingly, when defining __eq__() should also define __ne__() so that the operators act properly.

+12
source

Source: https://habr.com/ru/post/988508/


All Articles