Python variables are precisely name bindings to objects, not containers for values. This distinction is crucial for understanding mutation, function arguments, and aliasing. Unlike the common "box" metaphor, a Python name is simply a label attached to an object that exists independently in memory. Multiple names can point to the same object without creating duplicates. Assignment creates a new binding, effectively changing which object a name refers to. Mutation modifies the object an existing name points to, affecting all other names referencing that same object. Augmented assignment on mutable types, like lists, also mutates the object in place. However, augmented assignment on immutable types, like integers, rebinds the name to a new object. This behavior significantly impacts how data is passed into and modified by functions. The difference between creating a new object versus mutating an existing one within a function leads to distinct outcomes for the original variable. Understanding this model is essential for correctly predicting the behavior of code, especially in complex scenarios like default function arguments. The default argument [] creates a single list object at definition, which is then mutated across calls. Practicing code tracing exercises is recommended to build accuracy in applying this mental model.
[]creates a single list object at definition, which is then mutated across calls. Practicing code tracing exercises is recommended to build accuracy in applying this mental model.