我的代码:
class Test(object):
def __init__(self, number):
self.number = number
def __str__(self):
return "This number is {}".format(self.number)
def __add__(self, second):
self.number = self.number + second.number
return self
c1 = Test(1)
c2 = Test(2)
c2 = c1 + c2
print(c2)
print(c1)
输出:
This number is 3
This number is 3
我的麻烦是,当我尝试添加它们时,它会更改 c1 和 c2。我如何保持 c1 不变?