Understanding of references (pseudo pointers in python)

# Summary: Personal understanding, when referencing immutable variables, it will point to a new address as it changes
# When referencing a mutable variable, the position does not change as the variable changes

a = 1
b = a
print(b)
a = 2 # points to the new memory address
print(a)
print(b) # b will not change

c = 100
d = 100
print(id(c), id(d))


a = [1, 2]
b = a
print(a)
print(b)
a.append(3) # This refers to the same memory address
print(a) # b changes accordingly
print(b) # deep copy and shallow copy, different locations in memory

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325878362&siteId=291194637