Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e702481

Browse files
committed
Revert to ver 1.22, which was the version before the nested scopes
docs were introduced. This loses a few small changes, but Fred says that's okay.
1 parent 987ec90 commit e702481

1 file changed

Lines changed: 106 additions & 91 deletions

File tree

Doc/ref/ref4.tex

Lines changed: 106 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -24,121 +24,136 @@ \section{Code blocks, execution frames, and namespaces \label{execframes}}
2424
`\strong{-c}' option) is a code block. The file read by the built-in
2525
function \function{execfile()} is a code block. The string argument
2626
passed to the built-in function \function{eval()} and to the
27-
\keyword{exec}\stindex{exec} statement is a code block. And finally,
28-
the expression read and evaluated by the built-in function
29-
\function{input()} is a code block.
27+
\keyword{exec} statement is a code block. And finally, the expression
28+
read and evaluated by the built-in function \function{input()} is a
29+
code block.
3030

3131
A code block is executed in an execution frame. An \dfn{execution
3232
frame}\indexii{execution}{frame} contains some administrative
3333
information (used for debugging), determines where and how execution
3434
continues after the code block's execution has completed, and (perhaps
35-
most importantly) defines the environment in which names are resolved.
36-
37-
A \dfn{namespace}\indexii{namespace} is a mapping from names
38-
(identifiers) to objects. An \dfn{environment}\index{environment} is
39-
a hierarchical collection of the namespaces that are visible to a
40-
particular code block. Python namespaces are statically scoped in the
41-
tradition of Algol, but also has \keyword{global} statement that can
42-
be used to access the top-level namespace on the environment.
43-
44-
Names refers to objects. Names are introduced by name
45-
\dfn{binding}\indexii{binding}{name} operations. Each occurrence of a name
46-
in the program text refers to the binding of that name established in
47-
the innermost function namespace containing the use. Changing the
48-
mapping of a name to an object is called
49-
\dfn{rebinding}\indexii{rebinding}{name}; removing a name is
35+
most importantly) defines two namespaces, the local and the global
36+
namespace, that affect execution of the code block.
37+
38+
A \dfn{namespace}\index{namespace} is a mapping from names
39+
(identifiers) to objects. A particular namespace may be referenced by
40+
more than one execution frame, and from other places as well. Adding
41+
a name to a namespace is called \dfn{binding}\indexii{binding}{name} a
42+
name (to an object); changing the mapping of a name is called
43+
\dfn{rebinding}\indexii{rebinding}{name}; removing a name is
5044
\dfn{unbinding}\indexii{unbinding}{name}. Namespaces are functionally
5145
equivalent to dictionaries (and often implemented as dictionaries).
5246

53-
When a name is bound, a mapping is created in the \dfn{local
54-
namespace}\indexii{local}{namespace} of the execution frame unless the
55-
name is declared global. If a name binding operation occurs anywhere
56-
within a code block, all uses of the name within the block are treated
57-
as references to the local namespace. (Note: This can lead to errors
58-
when a name is used within a block before it is bound.)
59-
60-
The \dfn{global namespace}\indexii{global}{namespace} determines the
61-
place where names listed in \keyword{global}\stindex{global}
62-
statements are defined and searched. The global namespace of a block
63-
is the namespace of the module in which the block was defined.
64-
65-
If a name is used within a code block, but it is not bound there and
66-
is not declared global, it is a \dfn{free variable}
67-
\indexii{free}{variable}. A free variable is resolved using the
68-
nearest enclosing function block that has a binding for the name. If
69-
no such block exists, the name is resolved in the global namespace.
70-
71-
When a name is not found at all, a
72-
\exception{NameError}\withsubitem{(built-in
73-
exception)}{\ttindex{NameError}} exception is raised.
74-
75-
The local namespace of a class definition becomes the attribute
76-
dictionary of the class. If a block is contained within a class
77-
definition, the name bindings that occur in the containing class block
78-
are not visible to enclosed blocks.
79-
80-
The following constructs bind names: formal parameters to functions,
81-
\keyword{import} statements, class and function definitions (these bind
82-
the class or function name in the defining block), and identifiers
83-
occurring as the target of an assignment, in a \keyword{for} loop header
84-
(including list comprehensions), or in the second position of an
85-
\keyword{except} clause.
47+
The \dfn{local namespace}\indexii{local}{namespace} of an execution
48+
frame determines the default place where names are defined and
49+
searched. The
50+
\dfn{global namespace}\indexii{global}{namespace} determines the place
51+
where names listed in \keyword{global}\stindex{global} statements are
52+
defined and searched, and where names that are not bound anywhere in
53+
the current code block are searched.
8654

8755
Whether a name is local or global in a code block is determined by
8856
static inspection of the source text for the code block: in the
89-
absence of \keyword{global}\stindex{global} statements, a name that is
90-
bound anywhere in the code block is local in the entire code block;
91-
all other names are considered global. The \keyword{global} statement
92-
forces global interpretation of selected names throughout the code
93-
block.
94-
95-
The following constructs bind names: formal parameters to functions,
57+
absence of \keyword{global} statements, a name that is bound anywhere
58+
in the code block is local in the entire code block; all other names
59+
are considered global. The \keyword{global} statement forces global
60+
interpretation of selected names throughout the code block. The
61+
following constructs bind names: formal parameters to functions,
9662
\keyword{import} statements, class and function definitions (these
9763
bind the class or function name in the defining block), and targets
9864
that are identifiers if occurring in an assignment, \keyword{for} loop
9965
header, or in the second position of an \keyword{except} clause
100-
header. The \keyword{import} statement of the form ``\samp{from
101-
\ldots import *}''\stindex{from} binds all names defined in the
102-
imported module, except those beginning with an underscore. This form
103-
may only be used at the module level.
66+
header. Local names are searched only on the local namespace; global
67+
names are searched only in the global and built-in
68+
namespace.\footnote{
69+
If the code block contains \keyword{exec} statements or the
70+
construct ``\samp{from \ldots import *}'', the semantics of local
71+
names change: local name lookup first searches the local namespace,
72+
then the global namespace and the built-in namespace.}
10473

10574
A target occurring in a \keyword{del} statement is also considered bound
106-
for this purpose (though the actual semantics are to unbind the
107-
name). It is illegal to unbind a name that is referenced by an
108-
enclosing scope; the compiler will report a \exception{SyntaxError}.
75+
for this purpose (though the actual semantics are to ``unbind'' the
76+
name).
10977

11078
When a global name is not found in the global namespace, it is
11179
searched in the built-in namespace (which is actually the global
112-
namespace of the module \module{__builtin__}\refbimodindex{__builtin__}).
113-
The built-in namespace associated with the execution of a code block
114-
is actually found by looking up the name \code{__builtins__} in its
115-
global namespace; this should be a dictionary or a module (in the
116-
latter case the module's dictionary is used). Normally, the
117-
\code{__builtins__} namespace is the dictionary of the built-in module
118-
\module{__builtin__} (note: no `s'). If it isn't, restricted
119-
execution\indexii{restricted}{execution} mode is in effect.
120-
121-
The namespace for a module is automatically created the first time a
122-
module is imported. The main module for a script is always called
123-
\module{__main__}\refbimodindex{__main__}.
124-
125-
The \function{eval()}, \function{execfile()}, and \function{input()}
126-
functions and the \keyword{exec} statement do not have access to the
127-
full environment for resolving names. Names may be resolved in the
128-
local and global namespaces of the caller. Free variables are not
129-
resolved in the nearest enclosing namespaces, but in the global
130-
namespace.\footnote{This limitation occurs because the code that is
131-
executed by these operations is not available at the time the
132-
module is compiled.}
133-
The \keyword{exec} statement and the \function{eval()} and
80+
namespace of the module
81+
\module{__builtin__}\refbimodindex{__builtin__}). The built-in
82+
namespace associated with the execution of a code block is actually
83+
found by looking up the name \code{__builtins__} is its global
84+
namespace; this should be a dictionary or a module (in the latter case
85+
its dictionary is used). Normally, the \code{__builtins__} namespace
86+
is the dictionary of the built-in module \module{__builtin__} (note:
87+
no `s'); if it isn't, restricted
88+
execution\indexii{restricted}{execution} mode is in effect. When a
89+
name is not found at all, a
90+
\exception{NameError}\withsubitem{(built-in
91+
exception)}{\ttindex{NameError}} exception is raised.
92+
\stindex{from}
93+
\stindex{exec}
94+
\stindex{global}
95+
96+
The following table lists the meaning of the local and global
97+
namespace for various types of code blocks. The namespace for a
98+
particular module is automatically created when the module is first
99+
imported (i.e., when it is loaded). Note that in almost all cases,
100+
the global namespace is the namespace of the containing module ---
101+
scopes in Python do not nest!
102+
103+
\begin{tableiv}{l|l|l|l}{textrm}
104+
{Code block type}{Global namespace}{Local namespace}{Notes}
105+
\lineiv{Module}
106+
{n.s. for this module}
107+
{same as global}{}
108+
\lineiv{Script (file or command)}
109+
{n.s. for \module{__main__}\refbimodindex{__main__}}
110+
{same as global}{(1)}
111+
\lineiv{Interactive command}
112+
{n.s. for \module{__main__}\refbimodindex{__main__}}
113+
{same as global}{}
114+
\lineiv{Class definition}
115+
{global n.s. of containing block}
116+
{new n.s.}{}
117+
\lineiv{Function body}
118+
{global n.s. of containing block}
119+
{new n.s.}{(2)}
120+
\lineiv{String passed to \keyword{exec} statement}
121+
{global n.s. of containing block}
122+
{local n.s. of containing block}{(2), (3)}
123+
\lineiv{String passed to \function{eval()}}
124+
{global n.s. of caller}
125+
{local n.s. of caller}{(2), (3)}
126+
\lineiv{File read by \function{execfile()}}
127+
{global n.s. of caller}
128+
{local n.s. of caller}{(2), (3)}
129+
\lineiv{Expression read by \function{input()}}
130+
{global n.s. of caller}
131+
{local n.s. of caller}{}
132+
\end{tableiv}
133+
134+
Notes:
135+
136+
\begin{description}
137+
138+
\item[n.s.] means \emph{namespace}
139+
140+
\item[(1)] The main module for a script is always called
141+
\module{__main__}; ``the filename don't enter into it.''
142+
143+
\item[(2)] The global and local namespace for these can be
144+
overridden with optional extra arguments.
145+
146+
\item[(3)] The \keyword{exec} statement and the \function{eval()} and
134147
\function{execfile()} functions have optional arguments to override
135148
the global and local namespace. If only one namespace is specified,
136149
it is used for both.
137150

138-
The built-in functions \function{globals()} and \function{locals()}
139-
each return a dictionary, representing the current global and local
140-
namespace respectively. The effect of modifications to these
141-
dictionaries on the namespace are undefined.\footnote{
151+
\end{description}
152+
153+
The built-in functions \function{globals()} and \function{locals()} returns a
154+
dictionary representing the current global and local namespace,
155+
respectively. The effect of modifications to this dictionary on the
156+
namespace are undefined.\footnote{
142157
The current implementations return the dictionary actually used to
143158
implement the namespace, \emph{except} for functions, where the
144159
optimizer may cause the local namespace to be implemented

0 commit comments

Comments
 (0)