Python relationship between the rich and __ne__ comparative analysis method __eq__

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

Python rich comparison method comprises __lt__、__gt__、__le__、__ge__、__eq__和__ne__six methods, respectively: less than, greater than, less than or equal, greater than or equal, not equal, and equal to, the corresponding operation for the operator: <、>、<=、>=、==和!=. Does it like a normal digital operation as there is any relationship between these symbols it? Such as "less" if it contains a "less than"? ? There must be a relationship between the comparison symbols do contain this be the relationship between the rich and the comparative method __eq__ __ne__ analysis:
Python recommendations relationship between right and wrong __eq__ and __ne__, __ method of ne__ you should call __eq__ inverted to achieve completion, but if developers do not follow this requirement is actually possible.
Case Study:
"! =" 1, if the developer Pyhon in a custom class implements __eq__ and __ne__ these two methods, the "==" and two objects compare these two methods are called 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 a developer Pyhon in a custom class implements __eq__ method, __ne__ method is not implemented, then "==" and "! =" Are __eq__ method of comparing two objects calls are calls __eq__ comparison method, but the latter is negated __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 in a custom class implements __ne__ method, __eq__ method is not implemented, "! =" __Ne__ method call, and the system built eq method corresponding to "==" call old ape preliminary analysis should be calling "is", not confirmation. 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
>>>

3 From the above analysis can be known, and the internal Python claim __eq__ __ne__ are reciprocal relationship, and is not defined in the custom class __ne__ method when invoked __eq__ negated achieve the __ne__ Analyzing, this is because the object class is to realized as __ne__.

Old ape Python, with the old ape learn Python!
Blog address: https: //blog.csdn.net/LaoYuanPython

please support, thumbs up, comment and processing concern! Thank you!

Guess you like

Origin blog.csdn.net/LaoYuanPython/article/details/95218476