@@ -2,166 +2,147 @@ \chapter{Execution model \label{execmodel}}
22\index {execution model}
33
44
5- \section {Code blocks, execution frames, and namespaces \label {execframes } }
6- \index {code block}
5+ \section {Naming and binding \label {naming } }
6+ \indexii {code}{ block}
77\index {namespace}
8- \indexii {execution}{frame}
9-
10- A \dfn {code block}\indexii {code}{block} is a piece
11- of Python program text that can be executed as a unit, such as a
12- module, a class definition or a function body. Some code blocks (like
13- modules) are normally executed only once, others (like function
14- bodies) may be executed many times. Code blocks may textually contain
15- other code blocks. Code blocks may invoke other code blocks (that may
16- or may not be textually contained in them) as part of their execution,
17- e.g., by invoking (calling) a function.
18-
19- The following are code blocks: A module is a code block. A function
20- body is a code block. A class definition is a code block. Each
21- command typed interactively is a separate code block; a script file (a
22- file given as standard input to the interpreter or specified on the
23- interpreter command line the first argument) is a code block; a script
24- command (a command specified on the interpreter command line with the
25- `\strong {-c}' option) is a code block. The file read by the built-in
26- function \function {execfile()} is a code block. The string argument
27- passed to the built-in function \function {eval()} and to the
28- \keyword {exec} statement is a code block. And finally, the expression
29- read and evaluated by the built-in function \function {input()} is a
30- code block.
31-
32- A code block is executed in an execution frame. An \dfn {execution
33- frame}\indexii {execution}{frame} contains some administrative
34- information (used for debugging), determines where and how execution
35- continues after the code block's execution has completed, and (perhaps
36- most importantly) defines two namespaces, the local and the global
37- namespace, that affect execution of the code block.
38-
39- A \dfn {namespace}\index {namespace} is a mapping from names
40- (identifiers) to objects. A particular namespace may be referenced by
41- more than one execution frame, and from other places as well. Adding
42- a name to a namespace is called \dfn {binding}\indexii {binding}{name} a
43- name (to an object); changing the mapping of a name is called
44- \dfn {rebinding}\indexii {rebinding}{name}; removing a name is
45- \dfn {unbinding}\indexii {unbinding}{name}. Namespaces are functionally
46- equivalent to dictionaries (and often implemented as dictionaries).
47-
48- The \dfn {local namespace}\indexii {local}{namespace} of an execution
49- frame determines the default place where names are defined and
50- searched. The
51- \dfn {global namespace}\indexii {global}{namespace} determines the place
52- where names listed in \keyword {global}\stindex {global} statements are
53- defined and searched, and where names that are not bound anywhere in
54- the current code block are searched.
55-
56- Whether a name is local or global in a code block is determined by
57- static inspection of the source text for the code block: in the
58- absence of \keyword {global} statements, a name that is bound anywhere
59- in the code block is local in the entire code block; all other names
60- are considered global. The \keyword {global} statement forces global
61- interpretation of selected names throughout the code block. The
62- following constructs bind names: formal parameters to functions,
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 \dfn {scope}\index {scope} defines the visibility of a name within a
29+ block. If a local variable is defined in a block, it's scope includes
30+ that block. If the definition occurs in a function block, the scope
31+ extends to any blocks contained within the defining one, unless a
32+ contained block introduces a different binding for the name. The
33+ scope of names defined in a class block is limited to the class block;
34+ it does not extend to the code blocks of methods.
35+
36+ When a name is used in a code block, it is resolved using the nearest
37+ enclosing scope. The set of all such scopes visible to a code block
38+ is called the block's \dfn {environment}\index {environment}.
39+
40+ If a name is bound in a block, it is a local variable of that block.
41+ If a name is bound at the module level, it is a global variable. (The
42+ variables of the module code block are local and global.) If a
43+ variable is used in a code block but not defined there, it is a
44+ \dfn {free variable}\indexii {free}{variable}.
45+
46+ When a name is not found at all, a
47+ \exception {NameError}\withsubitem {(built-in
48+ exception)}{\ttindex {NameError}} exception is raised. If the name
49+ refers to a local variable that has not been bound, a
50+ \exception {UnboundLocalError}\ttindex {UnboundLocalError} exception is
51+ raised. \exception {UnboundLocalError} is a subclass of
52+ \exception {NameError}.
53+
54+ The following constructs bind names: formal parameters to functions,
6355\keyword {import} statements, class and function definitions (these
6456bind the class or function name in the defining block), and targets
6557that are identifiers if occurring in an assignment, \keyword {for} loop
6658header, or in the second position of an \keyword {except} clause
67- header. Local names are searched only on the local namespace; global
68- names are searched only in the global and built-in
69- namespace.\footnote {
70- If the code block contains \keyword {exec} statements or the
71- construct `` \samp {from \ldots import *}'' , the semantics of local
72- names change: local name lookup first searches the local namespace,
73- then the global namespace and the built-in namespace.}
59+ header. The \keyword {import} statement of the form `` \samp {from
60+ \ldots import *}'' \stindex {from} binds all names defined in the
61+ imported module, except those beginning with an underscore. This form
62+ may only be used at the module level.
7463
7564A target occurring in a \keyword {del} statement is also considered bound
76- for this purpose (though the actual semantics are to `` unbind'' the
77- name).
78-
79- When a global name is not found in the global namespace, it is
80- searched in the built-in namespace (which is actually the global
81- namespace of the module
82- \module {__builtin__}\refbimodindex {__builtin__}). The built-in
83- namespace associated with the execution of a code block is actually
84- found by looking up the name \code {__builtins__} in its global
85- namespace; this should be a dictionary or a module (in the latter case
86- its dictionary is used). Normally, the \code {__builtins__} namespace
87- is the dictionary of the built-in module \module {__builtin__} (note:
88- no `s'); if it isn't, restricted
89- execution\indexii {restricted}{execution} mode is in effect. When a
90- name is not found at all, a
91- \exception {NameError}\withsubitem {(built-in
92- exception)}{\ttindex {NameError}} exception is raised.
93- \stindex {from}
94- \stindex {exec}
95- \stindex {global}
96-
97- The following table lists the meaning of the local and global
98- namespace for various types of code blocks. The namespace for a
99- particular module is automatically created when the module is first
100- imported (i.e., when it is loaded). Note that in almost all cases,
101- the global namespace is the namespace of the containing module ---
102- scopes in Python do not nest!
103-
104- \begin {tableiv }{l|l|l|l}{textrm}
105- {Code block type}{Global namespace}{Local namespace}{Notes}
106- \lineiv {Module}
107- {n.s. for this module}
108- {same as global}{}
109- \lineiv {Script (file or command)}
110- {n.s. for \module {__main__}\refbimodindex {__main__}}
111- {same as global}{(1)}
112- \lineiv {Interactive command}
113- {n.s. for \module {__main__}\refbimodindex {__main__}}
114- {same as global}{}
115- \lineiv {Class definition}
116- {global n.s. of containing block}
117- {new n.s.}{}
118- \lineiv {Function body}
119- {global n.s. of containing block}
120- {new n.s.}{(2)}
121- \lineiv {String passed to \keyword {exec} statement}
122- {global n.s. of containing block}
123- {local n.s. of containing block}{(2), (3)}
124- \lineiv {String passed to \function {eval()}}
125- {global n.s. of caller}
126- {local n.s. of caller}{(2), (3)}
127- \lineiv {File read by \function {execfile()}}
128- {global n.s. of caller}
129- {local n.s. of caller}{(2), (3)}
130- \lineiv {Expression read by \function {input()}}
131- {global n.s. of caller}
132- {local n.s. of caller}{}
133- \end {tableiv }
134-
135- Notes:
136-
137- \begin {description }
138-
139- \item [n.s.] means \emph {namespace }
140-
141- \item [(1)] The main module for a script is always called
142- \module {__main__}; `` the filename don't enter into it.''
143-
144- \item [(2)] The global and local namespace for these can be
145- overridden with optional extra arguments.
146-
147- \item [(3)] The \keyword {exec} statement and the \function {eval()} and
65+ for this purpose (though the actual semantics are to unbind the
66+ name). It is illegal to unbind a name that is referenced by an
67+ enclosing scope; the compiler will report a \exception {SyntaxError}.
68+
69+ Each assignment or import statement occurs within a block defined by a
70+ class or function definition or at the module level (the top-level
71+ code block).
72+
73+ If a name binding operation occurs anywhere within a code block, all
74+ uses of the name within the block are treated as references to the
75+ current block. This can lead to errors when a name is used within a
76+ block before it is bound.
77+
78+ The previous rule is a subtle. Python lacks declarations and allows
79+ name binding operations to occur anywhere within a code block. The
80+ local variables of a code block can be determined by scanning the
81+ entire text of the block for name binding operations.
82+
83+ If the global statement occurs within a block, all uses of the name
84+ specified in the statement refer to the binding of that name in the
85+ top-level namespace. Names are resolved in the top-level namespace by
86+ searching the global namespace, i.e. the namespace of the module
87+ containing the code block, and the builtin namespace, the namespace of
88+ the module \module {__builtin__}. The global namespace is searched
89+ first. If the name is not found there, the builtin namespace is
90+ searched. The global statement must precede all uses of the name.
91+
92+ The built-in namespace associated with the execution of a code block
93+ is actually found by looking up the name \code {__builtins__} in its
94+ global namespace; this should be a dictionary or a module (in the
95+ latter case the module's dictionary is used). Normally, the
96+ \code {__builtins__} namespace is the dictionary of the built-in module
97+ \module {__builtin__} (note: no `s'). If it isn't, restricted
98+ execution\indexii {restricted}{execution} mode is in effect.
99+
100+ The namespace for a module is automatically created the first time a
101+ module is imported. The main module for a script is always called
102+ \module {__main__}\refbimodindex {__main__}.
103+
104+ The global statement has the same scope as a name binding operation
105+ in the same block. If the nearest enclosing scope for a free variable
106+ contains a global statement, the free variable is treated as a global.
107+
108+ A class definition is an executable statement that may use and define
109+ names. These references follow the normal rules for name resolution.
110+ The namespace of the class definition becomes the attribute dictionary
111+ of the class. Names defined at the class scope are not visible in
112+ methods.
113+
114+ \subsection {Interaction with dynamic features \label {dynamic-features } }
115+
116+ There are several cases where Python statements are illegal when
117+ used in conjunction with nested scopes that contain free
118+ variables.
119+
120+ If a variable is referenced in an enclosing scope, it is illegal
121+ to delete the name. An error will be reported at compile time.
122+
123+ If the wild card form of import --- \samp {import *} --- is used in a
124+ function and the function contains or is a nested block with free
125+ variables, the compiler will raise a SyntaxError.
126+
127+ If \keyword {exec} is used in a function and the function contains or
128+ is a nested block with free variables, the compiler will raise a
129+ \exception {SyntaxError} unless the exec explicitly specifies the local
130+ namespace for the \keyword {exec}. (In other words, \samp {exec obj}
131+ would be illegal, but \samp {exec obj in ns} would be legal.)
132+
133+ The \function {eval()}, \function {execfile()}, and \function {input()}
134+ functions and the \keyword {exec} statement do not have access to the
135+ full environment for resolving names. Names may be resolved in the
136+ local and global namespaces of the caller. Free variables are not
137+ resolved in the nearest enclosing namespace, but in the global
138+ namespace.\footnote {This limitation occurs because the code that is
139+ executed by these operations is not available at the time the
140+ module is compiled.}
141+ The \keyword {exec} statement and the \function {eval()} and
148142\function {execfile()} functions have optional arguments to override
149143the global and local namespace. If only one namespace is specified,
150144it is used for both.
151145
152- \end {description }
153-
154- The built-in functions \function {globals()} and \function {locals()} returns a
155- dictionary representing the current global and local namespace,
156- respectively. The effect of modifications to this dictionary on the
157- namespace are undefined.\footnote {
158- The current implementations return the dictionary actually used to
159- implement the namespace, \emph {except } for functions, where the
160- optimizer may cause the local namespace to be implemented
161- differently, and \function {locals()} returns a read-only
162- dictionary.}
163-
164-
165146\section {Exceptions \label {exceptions } }
166147\index {exception}
167148
0 commit comments