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