February 2006
Intermediate to advanced
648 pages
14h 53m
English
Each time a function executes, a new local namespace is created. This namespace contains the names of the function parameters, as well as the names of variables that are assigned inside the function body. When resolving names, the interpreter first searches the local namespace. If no match exists, it searches the global namespace. The global namespace for a function is always the module in which the function was defined. If the interpreter finds no match in the global namespace, it makes a final check in the built-in namespace. If this fails, a NameError exception is raised.
One peculiarity of namespaces is the manipulation of global variables from within a function. For example, consider the following code:
a = 42 def foo(): a ...