The original value changes after the python variable assignment is modified

 Python is a dynamic language, so the type and value of variables can change at runtime. When we assign a variable to another variable, we are actually passing the reference address of the variable to the new variable, which means that the old and new variables will point to the same location. So when changing the value of one of the variables, the value of the other variable is also changed.

import copy

a2 = [1, 2, 3]
b2 = a2
print(a2)
print(b2)
print(" ")

a2[0] = 4
print(a2)
print(b2)


a=[1, 2, 3]
b=copy.deepcopy(a)
a[0] = 4
print(" ")
print(a)
print(b)

Guess you like

Origin blog.csdn.net/u013288190/article/details/132124719