I have a snippet of Python code:
import copy
class Foo(object):
bar = dict()
def __init__(self, bar):
self.bar = bar
def loop(self):
backup = copy.deepcopy(self)
backup.bar[1] = "1"
for i in range(0, 10):
print "BACKUP BAR IS: ", backup.bar
self.bar[1] = "42"
self.bar = backup.bar
a = Foo({1:"0"})
a.loop()
This prints the BACKUP BAR IS: {1: '1'}first two times, but then starts to print the BACKUP BAR IS: {1: '42'}next eight times. Can someone tell me how and why this happens? Does it create a deepcopynew instance self? How does the value of backupbar change when we change the value of "self", which is "a", in this case?
Edit: some noted that the line self.bar = backup.barforces two dicts to point to each other. But if we do this:
a = {1:1}
b = {1:2}
c = {1:3}
a = b
a = c
a b , a, b, c c. a = c = {1:3}, b = {1:2}. , . , backup.bar , self.bar?