a = [1]
def do():
global a
b=a
print b
a[0] = 2
print b
do()
outputs: 1 2
I am pretty sure it has something to do with the fact that 'a' is a global list. Could someone please explain to me why the variable b changes when the global changes. And how i could possibly stop it from happening?
an extension to the question: how would you handle further nesting, such as:
a = []
b = []
def do():
global a, b
b.append(a[:])
print a, b
a[0][0] +=1
print a, b
a.append([1])
do()