Caffeinated Coder
Book an appointment
Lesson 12 · Collections and functions

Scope

Where can a name be used? Python keeps names in namespaces: one global namespace for the whole file, and a fresh local namespace every time a function is called. Step through a seven-line program, scope.py, and watch a function read a global name, make its own local one, hand a value back with return, and then forget its local names, so asking for one afterwards stops the program with a NameError.

12 min10 steps5 questionsBeginner
Fig 12 · Interactive
Loading figure…
or ← →

Quiz Unlocks after the last step.