The first 8.22 Python Case Detailed: Rewriting "relatively rich" approach control comparator logic

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 )

First, the case illustrates
this section defines a class of cars Car, including car class name carname, hundred kilometers oilcostper100km, price price three properties. Then implement __lt__、__gt__、__le__、__ge__four methods (use four methods, see the section "of 8.21 in Python __lt __, __ gt__ etc." "Research) method uses the" rich comparison "(" Rich Comparison ( HTTPS: // Blog. csdn.net/LaoYuanPython/article/details/95042104 ), to illustrate the problem, we will __lt__ comparison logic and comparison logic __gt__ and __ge__ of __le__ deliberately brought about the opposite, and rewrite methods details __repr__ formatted output. then we define two variables to compare the size of the instance, performed to see the effect.
II case Code

>>> class Car():
   def __init__(self,carname,oilcostper100km, price):
       self.carname,self.oilcostper100km,self.price = carname,oilcostper100km, price
   def __lt__(self,other):
       print("execute __lt__")
       return self.price<other.price
   def __le__(self,other):
       print("execute __le__")
       return self.price>other.price
   def __gt__(self,other):
       print("execute __gt__")
       return self.oilcostper100km>other.oilcostper100km

   def __ge__(self,other):
       print("execute __ge__")
       return self.oilcostper100km<other.oilcostper100km

   def __repr__(self):  #重写__repr__方法输出repr信息
       return  f"('{self.carname}',{self.oilcostper100km},{self.price})"

>>> car1,car2 = Car('爱丽舍',8,10),Car('凯美瑞',7,27)
>>> car1<car2,car1<=car2,car1>car2,car1>=car2
execute __lt__
execute __le__
execute __gt__
execute __ge__
(True, False, True, False)
>>>

Third, the case screenshots Here Insert Picture Description
four case studies

  1. 4 can know the comparison output, <, <=,>,> =, respectively called by __lt__、__le__、__gt__、__ge__four methods;
  2. Since __lt__、__le__the use of price compared, and a true comparison of two logical expressions are opposite, so the two output completely opposite, similarly __gt __, __ ge__ is the same, indicating that the object instance of a custom class Python when compared to the size of the , the size of the object determines rule are defined by the developer's own, it does not require "less" it must contain "less than", there is no logical relationship between them in the realization, __ gt__ __ge__ also true with the same Li __gt__ __lt__ and need not be the opposite result, the final logical overwrite should be confirmed according to the service required;
  3. Through the above cases, we also know that by overriding the rich comparison method, we can intercept the call object comparison logic;
  4. The actual rich comparison method also __eq__ and __ne__, these two methods to achieve the object class, can be used directly inherited, but can also be rewritten.

References in this section:
1, "in Section 8.21 Python __lt __, __ gt__ other" rich comparison "(" rich comparison ") method uses inquiry" ;
2, "Section 8.15, Python custom class rewritable __repr__ method"

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/95081700