|
|
|
|
|
by tsimionescu
677 days ago
|
|
> In the mutation example you suggest, if a reference to x isn't being passed into foo, how could foo modify x The important point is that it's not "a reference to x" that gets passed, it's a copy of x's value. x's value, like the value of all Python variables, is a reference to some object. The same thing applies to setting variables in Python in general: x = {1:2} # x is a new variable that references some dict
y = x # y is a new variable that references the same dict
y[1] = 7 # the dict referenced by x and y was modified
x = None # x no longer references the dict
print(y) # y still references the dict, so this will print {1:7}
y = None # now neither x nor y reference that dict; since y was the last reference to it, the dict's memory will be freed
|
|
Honestly, "x's value, like the value of all Python variables, is a reference to some object" makes me think it's more accurate to call Python pass-by-reference only.