55Execution model
66***************
77
8- .. index :: single: execution model
9-
10-
11- .. _naming :
12-
13- Naming and binding
14- ==================
15-
168.. index ::
9+ single: execution model
1710 pair: code; block
18- single: namespace
19- single: scope
2011
21- .. index ::
22- single: name
23- pair: binding; name
12+ .. _prog_structure :
2413
25- :dfn: `Names ` refer to objects. Names are introduced by name binding operations.
26- Each occurrence of a name in the program text refers to the :dfn: `binding ` of
27- that name established in the innermost function block containing the use.
14+ Structure of a programm
15+ =======================
2816
2917.. index :: block
3018
19+ A Python program is constructed from code blocks.
3120A :dfn: `block ` is a piece of Python program text that is executed as a unit.
3221The following are blocks: a module, a function body, and a class definition.
3322Each command typed interactively is a block. A script file (a file given as
@@ -43,43 +32,25 @@ A code block is executed in an :dfn:`execution frame`. A frame contains some
4332administrative information (used for debugging) and determines where and how
4433execution continues after the code block's execution has completed.
4534
46- .. index :: scope
47-
48- A :dfn: `scope ` defines the visibility of a name within a block. If a local
49- variable is defined in a block, its scope includes that block. If the
50- definition occurs in a function block, the scope extends to any blocks contained
51- within the defining one, unless a contained block introduces a different binding
52- for the name. The scope of names defined in a class block is limited to the
53- class block; it does not extend to the code blocks of methods -- this includes
54- comprehensions and generator expressions since they are implemented using a
55- function scope. This means that the following will fail::
56-
57- class A:
58- a = 42
59- b = list(a + i for i in range(10))
35+ .. _naming :
6036
61- .. index :: single: environment
37+ Naming and binding
38+ ==================
6239
63- When a name is used in a code block, it is resolved using the nearest enclosing
64- scope. The set of all such scopes visible to a code block is called the block's
65- :dfn: ` environment `.
40+ .. index ::
41+ single: namespace
42+ single: scope
6643
67- .. index :: pair: free; variable
44+ .. _ bind_names :
6845
69- If a name is bound in a block, it is a local variable of that block, unless
70- declared as :keyword: `nonlocal `. If a name is bound at the module level, it is
71- a global variable. (The variables of the module code block are local and
72- global.) If a variable is used in a code block but not defined there, it is a
73- :dfn: `free variable `.
46+ Binding of names
47+ ----------------
7448
7549.. index ::
76- single: NameError (built-in exception)
77- single: UnboundLocalError
50+ single: name
51+ pair: binding; name
7852
79- When a name is not found at all, a :exc: `NameError ` exception is raised. If the
80- name refers to a local variable that has not been bound, an
81- :exc: `UnboundLocalError ` exception is raised. :exc: `UnboundLocalError ` is a
82- subclass of :exc: `NameError `.
53+ :dfn: `Names ` refer to objects. Names are introduced by name binding operations.
8354
8455.. index :: statement: from
8556
@@ -99,6 +70,46 @@ this purpose (though the actual semantics are to unbind the name).
9970Each assignment or import statement occurs within a block defined by a class or
10071function definition or at the module level (the top-level code block).
10172
73+ .. index :: pair: free; variable
74+
75+ If a name is bound in a block, it is a local variable of that block, unless
76+ declared as :keyword: `nonlocal ` or :keyword: `global `. If a name is bound at
77+ the module level, it is a global variable. (The variables of the module code
78+ block are local and global.) If a variable is used in a code block but not
79+ defined there, it is a :dfn: `free variable `.
80+
81+ Each occurrence of a name in the program text refers to the :dfn: `binding ` of
82+ that name established by the following name resolution rules.
83+
84+ .. _resolve_names :
85+
86+ Resolution of names
87+ -------------------
88+
89+ .. index :: scope
90+
91+ A :dfn: `scope ` defines the visibility of a name within a block. If a local
92+ variable is defined in a block, its scope includes that block. If the
93+ definition occurs in a function block, the scope extends to any blocks contained
94+ within the defining one, unless a contained block introduces a different binding
95+ for the name.
96+
97+ .. index :: single: environment
98+
99+ When a name is used in a code block, it is resolved using the nearest enclosing
100+ scope. The set of all such scopes visible to a code block is called the block's
101+ :dfn: `environment `.
102+
103+ .. index ::
104+ single: NameError (built-in exception)
105+ single: UnboundLocalError
106+
107+ When a name is not found at all, a :exc: `NameError ` exception is raised.
108+ If the current scope is a function scope, and the name refers to a local
109+ variable that has not yet been bound to a value at the point where the name is
110+ used, an :exc: `UnboundLocalError ` exception is raised.
111+ :exc: `UnboundLocalError ` is a subclass of :exc: `NameError `.
112+
102113If a name binding operation occurs anywhere within a code block, all uses of the
103114name within the block are treated as references to the current block. This can
104115lead to errors when a name is used within a block before it is bound. This rule
@@ -115,7 +126,41 @@ global namespace is searched first. If the name is not found there, the
115126builtins namespace is searched. The :keyword: `global ` statement must precede
116127all uses of the name.
117128
118- .. XXX document "nonlocal" semantics here
129+ The :keyword: `global ` statement has the same scope as a name binding operation
130+ in the same block. If the nearest enclosing scope for a free variable contains
131+ a global statement, the free variable is treated as a global.
132+
133+ .. XXX say more about "nonlocal" semantics here
134+
135+ The :keyword: `nonlocal ` statement causes corresponding names to refer
136+ to previously bound variables in the nearest enclosing function scope.
137+ :exc: `SyntaxError ` is raised at compile time if the given name does not
138+ exist in any enclosing function scope.
139+
140+ .. index :: module: __main__
141+
142+ The namespace for a module is automatically created the first time a module is
143+ imported. The main module for a script is always called :mod: `__main__ `.
144+
145+ Class definition blocks and arguments to :func: `exec ` and :func: `eval ` are
146+ special in the context of name resolution.
147+ A class definition is an executable statement that may use and define names.
148+ These references follow the normal rules for name resolution with an exception
149+ that unbound local variables are looked up in the global namespace.
150+ The namespace of the class definition becomes the attribute dictionary of
151+ the class. The scope of names defined in a class block is limited to the
152+ class block; it does not extend to the code blocks of methods -- this includes
153+ comprehensions and generator expressions since they are implemented using a
154+ function scope. This means that the following will fail::
155+
156+ class A:
157+ a = 42
158+ b = list(a + i for i in range(10))
159+
160+ .. _restrict_exec :
161+
162+ Builtins and restricted execution
163+ ---------------------------------
119164
120165.. index :: pair: restricted; execution
121166
@@ -135,26 +180,20 @@ weak form of restricted execution.
135180 :keyword: `import ` the :mod: `builtins ` module and modify its
136181 attributes appropriately.
137182
138- .. index :: module: __main__
139-
140- The namespace for a module is automatically created the first time a module is
141- imported. The main module for a script is always called :mod: `__main__ `.
142-
143- The :keyword: `global ` statement has the same scope as a name binding operation
144- in the same block. If the nearest enclosing scope for a free variable contains
145- a global statement, the free variable is treated as a global.
146-
147- A class definition is an executable statement that may use and define names.
148- These references follow the normal rules for name resolution. The namespace of
149- the class definition becomes the attribute dictionary of the class. Names
150- defined at the class scope are not visible in methods.
151-
152-
153183.. _dynamic-features :
154184
155185Interaction with dynamic features
156186---------------------------------
157187
188+ Name resolution of free variables occurs at runtime, not at compile time.
189+ This means that the following code will print 42::
190+
191+ i = 10
192+ def f():
193+ print(i)
194+ i = 42
195+ f()
196+
158197There are several cases where Python statements are illegal when used in
159198conjunction with nested scopes that contain free variables.
160199
0 commit comments