source: trunk/essentials/dev-lang/python/Doc/ref/ref4.tex@ 3315

Last change on this file since 3315 was 3225, checked in by bird, 19 years ago

Python 2.5

File size: 10.5 KB
Line 
1\chapter{Execution model \label{execmodel}}
2\index{execution model}
3
4
5\section{Naming and binding \label{naming}}
6\indexii{code}{block}
7\index{namespace}
8\index{scope}
9
10\dfn{Names}\index{name} refer to objects. Names are introduced by
11name binding operations. Each occurrence of a name in the program
12text refers to the \dfn{binding}\indexii{binding}{name} of that name
13established in the innermost function block containing the use.
14
15A \dfn{block}\index{block} is a piece of Python program text that is
16executed as a unit. The following are blocks: a module, a function
17body, and a class definition. Each command typed interactively is a
18block. A script file (a file given as standard input to the
19interpreter or specified on the interpreter command line the first
20argument) is a code block. A script command (a command specified on
21the interpreter command line with the `\strong{-c}' option) is a code
22block. The file read by the built-in function \function{execfile()}
23is a code block. The string argument passed to the built-in function
24\function{eval()} and to the \keyword{exec} statement is a code block.
25The expression read and evaluated by the built-in function
26\function{input()} is a code block.
27
28A code block is executed in an \dfn{execution
29frame}\indexii{execution}{frame}. A frame contains some
30administrative information (used for debugging) and determines where
31and how execution continues after the code block's execution has
32completed.
33
34A \dfn{scope}\index{scope} defines the visibility of a name within a
35block. If a local variable is defined in a block, its scope includes
36that block. If the definition occurs in a function block, the scope
37extends to any blocks contained within the defining one, unless a
38contained block introduces a different binding for the name. The
39scope of names defined in a class block is limited to the class block;
40it does not extend to the code blocks of methods.
41
42When a name is used in a code block, it is resolved using the nearest
43enclosing scope. The set of all such scopes visible to a code block
44is called the block's \dfn{environment}\index{environment}.
45
46If a name is bound in a block, it is a local variable of that block.
47If a name is bound at the module level, it is a global variable. (The
48variables of the module code block are local and global.) If a
49variable is used in a code block but not defined there, it is a
50\dfn{free variable}\indexii{free}{variable}.
51