Variables and Memory Management
A structured, from-first-principles guide to how Python variables and memory really work — references, stack vs heap, copying, garbage collection, memory optimization, scope, function argument passing, and real-world DevOps examples.
Explore Variables and Memory Management
6.1 Introduction to Variables
What a variable really is underneath the syntax, why that matters for memory behavior, and a map of the deeper reference, memory, and scope topics this chapter covers.
Read guide6.2 Objects and Variable References
Why a Python variable is a label pointing at an object rather than a box holding a value, what that means for reference assignment, and why shared references to mutable objects are a common source of bugs.
Read guide6.3 Memory Management: Stack vs Heap
How CPython splits memory between the call stack (frame references) and the heap (actual object data), and why that split explains reference semantics when passing variables into functions.
Read guide6.4 Assignment Operations
Why all assignment in Python is reference assignment, why there's no true separate value-assignment mechanism, and how that plays out differently for mutable versus immutable objects.
Read guide6.5 Copying Objects
The difference between assignment, a shallow copy, and a deep copy in Python, when each is appropriate, and how the copy module's copy() and deepcopy() behave differently on nested mutable objects.
Read guide6.6 Garbage Collection
How CPython's reference-counting memory manager works, why a supplementary cyclic garbage collector exists for reference cycles, and how to use del and the gc module.
Read guide6.7 Memory Optimization
CPython's automatic object-reuse optimizations — string interning and small integer caching — why they exist, and why they're implementation details you should never rely on for correctness.
Read guide6.8 Variable Scope
Local, global, and enclosing scope in Python, the global and nonlocal keywords, and the LEGB rule Python uses to resolve any name.
Read guide6.9 Lifetime of Variables
When a Python variable's lifetime begins and ends, how the del statement differs from letting a scope exit naturally, and how lifetime connects to garbage collection.
Read guide6.10 Variables in Functions
How Python passes arguments into functions, why mutable and immutable arguments behave differently when modified inside a function, and the return-value pattern that follows from it.
Read guide6.11 Variables in DevOps
How configuration variables, environment variables, secrets, AWS credentials, and Terraform variables are managed in real DevOps Python scripts, instead of hardcoding environment-specific values.
Read guide6.12 Common Mistakes
The most common variable and memory-related bugs in Python — UnboundLocalError, shared mutable objects, variable shadowing, and assuming Python has block scope.
Read guide6.13 Best Practices
Practical habits for writing memory-safe, readable Python — meaningful names, avoiding global state, using constants, and memory-efficient coding for long-running scripts.
Read guide6.14 Interview Questions
Frequently asked and scenario-based Python interview questions covering references, shallow vs deep copy, UnboundLocalError, the LEGB rule, and reference counting vs garbage collection.
Read guide6.15 Hands-on Exercises
Practice programs and mini projects for reinforcing Python variable and memory management concepts — UnboundLocalError, shallow vs deep copy, closures, environment-based configuration, and reference tracing.
Read guideVariables and Memory: Chapter Practice
Apply variables and memory with a mini lab and knowledge check.
Read guide