According to the python tutorial, functions look for variable names in the symbol tables of enclosing functions before looking for global functions:
The execution of a function introduces a new symbol table used for the local variables of the function. More precisely, all variable assignments in a function store the value in the local symbol table; whereas variable references first look in the local symbol table, then in the local symbol tables of enclosing functions, then in the global symbol table, and finally in the table of built-in names. Thus, global variables cannot be directly assigned a value within a function (unless named in a global statement), although they may be referenced.
What exactly does "enclosing function" mean, and when is it used?
I see the following code prints 10 when called
def parent_function():
y=10
def child_function():
print y
child_function()
However, calling child_function() alone produces an error. Are enclosing functions used frequently?