Analysis of the relationship between the Python comparison method __eq__ and __ne__

Original link: https://blog.csdn.net/LaoYuanPython/article/details/95218476

Python's rich comparison methods include __lt__, __gt__, __le__, __ge__, __eq__ and __ne__ six methods, which respectively represent: less than, greater than, less than or equal to, greater than or equal to, equal to, and not equal to, and the corresponding operators are: <, >, <=, >=, ==, and != . So is there an association relationship between these symbols like ordinary numerical operations? For example, does "less than or equal" include "less than"? Is there a necessary containment relationship between comparison symbols? This time, the relationship between the rich comparison methods __eq__ and __ne__ is analyzed:
Python recommends the right and wrong relationship between __eq__ and __ne__, the __ne__ method It should be done by calling __eq__ inverted during implementation, but it is actually possible if the developer does not follow this requirement.
Case study:
1. If the developer Pyhon implements the two methods __eq__ and __ne__ in the custom class, the two objects of "==" and "!=" call these two methods respectively Compare. code show as below:

>>> class Car():
   def __init__(self,carname,oilcp100km, price):
       self.carname,self.oilcp100km,self.price = carname,oilcp100km, price

   def __eq__(self,other):
       print("execute __eq__")
       return self.price==other.price
   def __ne__(self,other):
       print("execute __ne__")
       return self.price!=other.price

>>> car1,car2,car3,car4 = Car('爱丽舍',8,10),Car('凯美瑞',7,27),Car('爱丽舍',8,10),Car('途观',12,27)
>>> car1==car2
execute __eq__
False
>>> car1!=car2
execute __ne__
True
>>> car2==car4
execute __eq__
True
>>>

2. If the developer Pyhon implements the __eq__ method in the custom class, but does not implement the __ne__ method, then "==" and "!=" are both __eq__ methods, and both called objects are compared and called The __eq__ method is compared, but the latter is the inverse of __eq__. code show as below:

>>> class Car():
   def __init__(self,carname,oilcp100km, price):
       self.carname,self.oilcp100km,self.price = carname,oilcp100km, price

   def __eq__(self,other):
       print("execute __eq__")
       return self.price==other.price

>>> car1,car2,car3,car4 = Car('爱丽舍',8,10),Car('凯美瑞',7,27),Car('爱丽舍',8,10),Car('途观',12,27)
>>> car1==car2
execute __eq__
False
>>> car1!=car2
execute __eq__
True
>>>

3. If the developer Pyhon implements the __ne__ method in the custom class, but does not implement the __eq__ method, "!=" calls the __ne__ method, and eq calls the system's built-in "==" corresponding method , The initial analysis of the old ape should be to call "is", but has not yet verified. code show as below:

>>> class Car():
   def __init__(self,carname,oilcp100km, price):
       self.carname,self.oilcp100km,self.price = carname,oilcp100km, price
   
   def __ne__(self,other):
       print("execute __ne__")
       return self.price!=other.price

>>> car1,car2,car3,car4 = Car('爱丽舍',8,10),Car('凯美瑞',7,27),Car('爱丽舍',8,10),Car('途观',12,27)
>>> car1!=car3
execute __ne__
False
>>> car1==car3
False
>>> car1==car1
True
>>>

From the above three analysis processes, we can know that Python internally requires __eq__ and __ne__ to be mutually inverse, and when the __ne__ method is not defined in the custom class, call __eq__ to negate the realization of __ne__ Judging, this is because the __ne__ method in the object class is implemented in this way.

The reprinted article will advertise for him

LaoYuan Python, learn Python from
LaoYuan Python! Blog address: https://blog.csdn.net/LaoYuanPython,

please support, like, comment and follow! Thank you!

 

Guess you like

Origin blog.csdn.net/Answer3664/article/details/103968807