A code block is a piece of Python program text that can be executed as a unit, such as a module, a class definition or a function body. Some code blocks (like modules) are executed only once, others (like function bodies) may be executed many times. Code blocks may textually contain other code blocks. Code blocks may invoke other code blocks (that may or may not be textually contained in them) as part of their execution, e.g. by invoking (calling) a function.
The following are code blocks: A module is a code block. A function
body is a code block. A class definition is a code block. Each
command typed interactively is a separate code block; a script file is
a code block. The string argument passed to the built-in function
eval
and to the exec
statement are code blocks.
And finally, the
expression read and evaluated by the built-in function input
is
a code block.
A code block is executed in an execution frame. An execution frame contains some administrative information (used for debugging), determines where and how execution continues after the code block's execution has completed, and (perhaps most importantly) defines two name spaces, the local and the global name space, that affect execution of the code block.
A name space is a mapping from names (identifiers) to objects. A particular name space may be referenced by more than one execution frame, and from other places as well. Adding a name to a name space is called binding a name (to an object); changing the mapping of a name is called rebinding; removing a name is unbinding. Name spaces are functionally equivalent to dictionaries.
The local name space of an execution frame determines the default
place where names are defined and searched. The global name
space determines the place where names listed in global
statements are defined and searched, and where names that are not
explicitly bound in the current code block are searched.
Whether a name is local or global in a code block is determined by
static inspection of the source text for the code block: in the
absence of global
statements, a name that is bound anywhere in
the code block is local in the entire code block; all other names are
considered global. The global
statement forces global
interpretation of selected names throughout the code block. The
following constructs bind names: formal parameters, import
statements, class and function definitions (these bind the class or
function name), and targets that are identifiers if occurring in an
assignment, for
loop header, or except
clause header.
A target occurring in a del
statement is also considered bound
for this purpose (though the actual semantics are to ``unbind'' the
name).
When a global name is not found in the global name space, it is
searched in the list of ``built-in'' names (which is actually the
global name space of the module __builtin__
). When a name is not
found at all, the NameError
exception is raised.
The following table lists the meaning of the local and global name space for various types of code blocks. The name space for a particular module is automatically created when the module is first referenced. Note that in almost all cases, the global name space is the name space of the containing module --- scopes in Python do not nest!
Notes:
The built-in functions globals()
and locals()
returns a
dictionary representing the current global and local name space,
respectively. The effect of modifications to this dictionary on the
name space are undefined.