Python还提供了比较的神奇方法。 __lt__ 为 < __le__ 为 <= __eq__ 为 == __ne__ 为 != __gt__ 为 > __ge__ 为 >=
class SpecialString:
def __init__(self, cont):
self.cont = cont
def __gt__(self, other):
for index in range(len(other.cont)+1):
result = other.cont[:index] + ">" + self.cont
result += ">" + other.cont[index:]
print(result)
spam = SpecialString("spam")
eggs = SpecialString("eggs")
spam > eggs
结果:
>>> >spam>eggs e>spam>ggs eg>spam>gs egg>spam>s eggs>spam> >>>