Python variables are not "references" in the C++ sense. Rather, they are simply local names bound to an object at some arbitrary location in memory. If that object is itself mutable, changes to it will be visible in other scopes that have bound a name to the object. Many primitive types (including bool
, int
, str
, and tuple
) are immutable however. You cannot change their value in-place; rather, you assign a new value to the same name in your local scope.
In fact, almost any time* you see code of the form foo = X
, it means that the name foo
is being assigned a new value (X
) within your current local namespace, not that a location in memory named by foo
is having its internal pointer updated to refer instead to the location of X
.
*- the only exception to this in Python is setter methods for properties, which may allow you to write obj.foo = X
and have it rewritten in the background to instead call a method like obj.setFoo(X)
.