1. 加法 add+ 号前面的对象会触发加号,即+号前面的对象会传给self,+ 号后面的对象传给other
class MyStr:
def __init__(self, value):
self.value = str(value)
def __add__(self, other):
print("对象使用+号会触发add方法", self.value, other.value)
return self.value + other.value
obj = MyStr(1)
obj2 = MyStr(2)
print(obj + obj2) # + 号前面的对象会触发加号,即+号前面的对象会传给self,+ 号后面的对象传给other