I was looking online and I came across these 3 segments of code. The question is to predict the output and explain why.
Example 1:
x = 42
y = x
x = x + 1
print x
print y
Output Example 1:
43
42
Example 2:
x = [1, 2, 3]
y = x
x[0] = 4
print x
print y
Output Example 2:
[4, 2, 3]
[4, 2, 3]
Example 3:
x = ['foo', [1,2,3], 10.4]
y = list(x) # or x[:]
y[0] = 'fooooooo'
y[1][0] = 4
print x
print y
Output Example 3:
['foo', [4, 2, 3], 10.4]
['fooooooo', [4, 2, 3], 10.4]
For the most part I understand that this is a question about shallow and deep copying but I can't seem to wrap my head around this simple thing. In Example 2 at x[0] = 4
I understand that x
and y
are pointing to the same object and therefore both x
and y
are redefined and not just x
in this statement. But then why in Example 3 does this same logic not follow. At y[0] = 'fooooooo'
this should cause x
and y
to be redefined, just like the following line at y[1][0] = 4
causes both x
and y
to be redefined together.