Python definition _ _ lt__ method does not define _ _ gt__, automatically calls _ _ lt__? when > is used

class SavingsAccount(object):
    def __init__(self, name, pin, balance = 0.0):
        self._name = name
        self._pin = pin
        self._balance = balance
        
    def __lt__(self,other):
        print("this is <")
        return self._name < other._name

s1 = SavingsAccount("Ken","1000",0)
s2 = SavingsAccount("Bill", "1001",30)  

s1<s2
this is <
False

s1>s2
this is <
True

there is no problem calling the _ _ lt__ method when S1 < S2. The problem is that when calling >, it also seems to have called _ _ lt__, but the result seems to be a non-operation. What is the feature of python? Under what circumstances will this result occur

Mar.19,2021

refer to python.org/dev/peps/pep-0207/" rel=" nofollow noreferrer "> PEP207 :

If the object on the left side of the operator does not define an appropriate rich comparison operator (either at the C level or with one of the special methods, then the comparison is reversed, and the right hand operator is called with the opposite operator, and the two objects are swapped. This assumes that a < b and b > an are equivalent, as are a < = b and b > = a, and that = = and! = are commutative (rooma = = b if and only if b = = a).

python3 assumes that < and are the opposite operations. If one of them is not defined, the defined one is called when the other is used, just swapping the positions of the compared objects. The same features also occur in < = and > = and = and ! = .

Menu