Take a look to the following code snippet:
class MyObj(object):
name = ""
def __init__(self, name):
self.name = name
v = [ {} ] * 2
def f(index):
v[index]['surface'] = MyObj('test')
v[index]['num'] = 3
if __name__ == '__main__':
f(0)
f(1)
v[0]['num'] = 4
print v[1]['num']
What I expected to get as output of the last line is a 3
; however it prints out 4
. So it should mean that the new object is created always at the same reference address.
How can I avoid this behaviour? (i.e. how can I make the above code prints 4?)