Python += calls __add__ and rebinds to a new object

Python += calls __add__ and rebinds to a new object

If you invoke +=, Python can use __add__.

MyClass implements __add__ (calling print for debugging):

m1 = MyClass(10)
m2 = MyClass(20)
m1 += m2  # prints "Now in MyClass.__add__"

m1 now refers to a new object, and its repr is:

MyClass instance, vars(self)={‘x’: 30}