6.15 Hands-on Exercises
Practice Programs Write a function demonstrating UnboundLocalError, then fix it using the global keyword. Given a nested list, show the …
Practice Programs Write a function demonstrating UnboundLocalError, then fix it using the global keyword. Given a nested list, show the …
Frequently Asked Questions What is the difference between a variable and an object in Python? Explain the difference between is and == with …
Meaningful Names Choose names that describe what a variable holds (user_count, not uc) — this is the single highest-leverage readability …
UnboundLocalError What Goes Wrong? Referencing a variable inside a function before assigning it, when that same name is also assigned …
DevOps work is full of environment-specific values — variables, in every sense (Python, OS environment, and Terraform), are how …
Passing Variables What Is It? Python passes arguments by “assignment” — the parameter name inside the function becomes a new …
Creation A variable’s lifetime begins the moment it’s first assigned a value. Usage A variable remains usable for as long as its …
flowchart TD B["Built-in\nlen, print, range, ... — always available"] Gs["Global\nnames at module level"] E["Enclosing\nnames in an outer …
Object Reuse Where possible, CPython reuses existing immutable objects instead of allocating new ones for identical values. The two …
Reference Counting What Is It? CPython’s primary memory-management mechanism, introduced in 5.12 Memory Representation — every object …
When shared references aren’t what you want (see 6.2 Objects and Variable References), Python offers three distinct ways to copy a …
Reference Assignment b = a makes b point at the exact same object a does — no data is copied, as covered in 6.2 Objects and Variable …
Stack vs Heap What Is It? The stack holds function call frames — each frame stores its local variable names and the references they point …
Objects, Briefly Every value a variable can refer to — numbers, strings, functions, even classes — is an object with its own identity, type, …
flowchart TD V["Variable"] V --> R["References\nlabel, not a box"] V --> M["Stack vs Heap\nwhere things live"] V --> C["Copying\nshallow vs …
Context: Your Python service deployed in Kubernetes (256 MB memory limit) is being OOM-killed every few hours. The service reads and …
Context: You deployed a Python microservice that processes incoming webhook events. After a few hours of running in production, memory usage …