I'd like to clarify a simple pointer mechanic in Python. Consider the following:
u = [1,2]
v = [u,3]
print(v)
u[0] = 100
print(v)
Then the result is
[[1,2],3]
[[100,2],3]
Meanwhile if we perform
u = [1,2]
v = [u,3]
print(v)
u = [100,2]
print(v)
Then the result is
[[1,2],3]
[[1,2],3]
I think this happens because in the first code, the pointer that u
stores doesn't change throughout while in the second code, the pointer that u
stores changed from the declaration u=[100,2]
but the declaration v=[u,3]
stored the initial pointer itself and not the variable u
.
Is this the correct explanation as to why this happened?