How Python Actually Executes Your Code: Namespaces, Frames, and References
When Python runs a file, it first compiles source code to bytecode, creates a global namespace dictionary, and then executes instructions top to bottom. Every variable name is simply a key in that namespace dictionary pointing to an object's memory address, not a container holding a value directly. Each function call generates a new stack frame with its own local namespace, which is destroyed once the function returns, explaining why local variables do not persist. Because names are references to objects, assigning one variable to another makes both point to the same memory address, so mutating the object through either name affects both. Distinguishing between operations that modify an existing object in place and those that create a new one is the key to predicting Python's behavior around assignment and mutation.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in