Your problem is covered in the docs here
https://docs.python.org/3/reference/datamodel.html#object.iadd
For instance, if x
is an instance of a class with an __iadd__()
method, x += y
is equivalent to x = x.__iadd__(y)
.
So you see data
is being rebound (to itself in this case), but it still counts as an assignment.
Since data += data2
appears inside a function scope where data
has not been declared to be global
, a local variable called data
is assumed.
def foo():
data.extend(data2) # ok -- global data variable here
def bar():
data = data.__iadd__(data2) # not ok -- local data variable here
def baz():
data += data2 # not ok -- equivalent to bar()