Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 run it with Python the output is True. My question is

  1. the __ne__ method is not implemented, does Python fall on a default one?
  2. if Python fall on a default method to determine whether two objects are equal or not, shouldn't it call __eq__ and then negate the result?
like image 570
Bob Fang Avatar asked Jun 04 '15 11:06

Bob Fang


1 Answers

From the docs:

There are no implied relationships among the comparison operators. The truth of x==y does not imply that x!=y is false. Accordingly, when defining __eq__(), one should also define __ne__() so that the operators will behave as expected.

like image 118
Ami Tavory Avatar answered Sep 18 '22 04:09

Ami Tavory