i really do not understand why the behaviours are not the same.
I have read https://stackoverflow.com/a/9172325/6581137 but i do not understand why iadd and += would produce differents bytecodes.
Using Python 3.6
class flist:
def __init__(self):
self.a = 1
def __iadd__(self, nb):
self.a = self.a + nb
def __repr__(self):
return str(self.a)
t = (flist(), flist(), flist())
print(t)
try:
t[1].__iadd__(2)
#t[1] += 2
except:
print("exception")
print(t)
Output: t[1] is incremented by 2, normal.
(1, 1, 1)
(1, 3, 1)
But,
class flist:
def __init__(self):
self.a = 1
def __iadd__(self, nb):
self.a = self.a + nb
def __repr__(self):
return str(self.a)
t = (flist(), flist(), flist())
print(t)
try:
#t[1].__iadd__(2)
t[1] += 2
except:
print("exception")
print(t)
Output: t[1] is incremented by 2, but it raises an exception ?
(1, 1, 1)
exception
(1, 3, 1)
Thanks