Suppose I have the following code:
class C:
def __init__(self, data):
self.data=data #default
x=C(1)
y=C(2)
for obj in [x, y]:
obj.data+=1 # manipulate object referenced to by x or y
obj=C(999) # create a new object
print obj.data
print x.data
print y.data
the output is:
999 999 2 3
If I understand correctly, in the for loop the objects are manipulated in obj.data+=1
but when creating a new object in obj=C(999)
the temporary alias obj
and original x and y are no longer pointing to same object.
So my question: how to iterate through multiple known objects and be able to assign to them new objects? I my example it would mean that x and y will point to newly created objects in the for loop so the output will be:
999 999 999 999