Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
b18a93b4
Commit
b18a93b4
authored
Jul 23, 1998
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Changes copied from the FrameMaker version (hardly anything original).
parent
3bd9ab0e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
99 additions
and
88 deletions
+99
-88
Doc/ref/ref4.tex
Doc/ref/ref4.tex
+99
-88
No files found.
Doc/ref/ref4.tex
View file @
b18a93b4
\chapter
{
Execution model
}
\chapter
{
Execution model
}
\index
{
execution model
}
\index
{
execution model
}
\section
{
Code blocks, execution frames, and name
spaces
}
\label
{
execframes
}
\section
{
Code blocks, execution frames, and namespaces
}
\label
{
execframes
}
\index
{
code block
}
\index
{
code block
}
\indexii
{
execution
}{
frame
}
\indexii
{
execution
}{
frame
}
\index
{
name
space
}
\index
{
namespace
}
A
{
\em
code block
}
is a piece of Python program text that can be
A
{
\em
code block
}
is a piece of Python program text that can be
executed as a unit, such as a module, a class definition or a function
executed as a unit, such as a module, a class definition or a function
body. Some code blocks (like modules) are executed only once, others
body. Some code blocks (like modules) are
normally
executed only once, others
(like function bodies) may be executed many times. Code blocks may
(like function bodies) may be executed many times. Code blocks may
textually contain other code blocks. Code blocks may invoke other
textually contain other code blocks. Code blocks may invoke other
code blocks (that may or may not be textually contained in them) as
code blocks (that may or may not be textually contained in them) as
...
@@ -16,96 +16,113 @@ part of their execution, e.g. by invoking (calling) a function.
...
@@ -16,96 +16,113 @@ part of their execution, e.g. by invoking (calling) a function.
\index
{
code block
}
\index
{
code block
}
\indexii
{
code
}{
block
}
\indexii
{
code
}{
block
}
The following are code blocks:
A module is a code block. A function
The following are code blocks: A module is a code block. A function
body is a code block. A class definition is a code block. Each
body is a code block. A class definition is a code block. Each
command typed interactively is a separate code block; a script file is
command typed interactively is a separate code block; a script file (a
a code block. The string argument passed to the built-in function
file given as standard input to the interpreter or specified on the
\function
{
eval()
}
and to the
\keyword
{
exec
}
statement are code blocks.
interpreter command line the first argument) is a code block; a script
And finally, the expression read and evaluated by the built-in
command (a command specified on the interpreter command line with the
function
\function
{
input()
}
is a code block.
`
\code
{
-c
}
' option) is a code block. The file read by the built-in
function
\function
{
execfile()
}
is a code block. The string argument
passed to the built-in function
\function
{
eval()
}
and to the
\keyword
{
exec
}
statement is a code block. And finally, the expression
read and evaluated by the built-in function
\function
{
input()
}
is a
code block.
A code block is executed in an execution frame. An
{
\em
execution
A code block is executed in an execution frame. An
{
\em
execution
frame
}
contains some administrative information (used for debugging),
frame
}
contains some administrative information (used for debugging),
determines where and how execution continues after the code block's
determines where and how execution continues after the code block's
execution has completed, and (perhaps most importantly) defines two
execution has completed, and (perhaps most importantly) defines two
name
spaces, the local and the global name
space, that affect
name
spaces, the local and the global name
space, that affect
execution of the code block.
execution of the code block.
\indexii
{
execution
}{
frame
}
\indexii
{
execution
}{
frame
}
A
{
\em
name
space
}
is a mapping from names (identifiers) to objects.
A
{
\em
namespace
}
is a mapping from names (identifiers) to objects.
A particular name
space may be referenced by more than one execution
A particular namespace may be referenced by more than one execution
frame, and from other places as well. Adding a name to a name
space
frame, and from other places as well. Adding a name to a namespace
is called
{
\em
binding
}
a name (to an object); changing the mapping of
is called
{
\em
binding
}
a name (to an object); changing the mapping of
a name is called
{
\em
rebinding
}
; removing a name is
{
\em
unbinding
}
.
a name is called
{
\em
rebinding
}
; removing a name is
{
\em
unbinding
}
.
Name spaces are functionally equivalent to dictionaries.
Namespaces are functionally equivalent to dictionaries (and often
\index
{
name space
}
implemented as dictionaries).
\index
{
namespace
}
\indexii
{
binding
}{
name
}
\indexii
{
binding
}{
name
}
\indexii
{
rebinding
}{
name
}
\indexii
{
rebinding
}{
name
}
\indexii
{
unbinding
}{
name
}
\indexii
{
unbinding
}{
name
}
The
{
\em
local name
space
}
of an execution frame determines the default
The
{
\em
local namespace
}
of an execution frame determines the default
place where names are defined and searched. The
{
\em
global
name
place where names are defined and searched. The
{
\em
global
space
}
determines the place where names listed in
\keyword
{
global
}
name
space
}
determines the place where names listed in
\keyword
{
global
}
statements are defined and searched, and where names that are not
statements are defined and searched, and where names that are not
explicitly bound
in the current code block are searched.
bound anywhere
in the current code block are searched.
\indexii
{
local
}{
name
space
}
\indexii
{
local
}{
namespace
}
\indexii
{
global
}{
name
space
}
\indexii
{
global
}{
namespace
}
\stindex
{
global
}
\stindex
{
global
}
Whether a name is local or global in a code block is determined by
Whether a name is local or global in a code block is determined by
static inspection of the source text for the code block: in the
static inspection of the source text for the code block: in the
absence of
\keyword
{
global
}
statements, a name that is bound anywhere
in
absence of
\keyword
{
global
}
statements, a name that is bound anywhere
the code block is local in the entire code block; all other names are
in the code block is local in the entire code block; all other names
considered global. The
\keyword
{
global
}
statement forces global
are
considered global. The
\keyword
{
global
}
statement forces global
interpretation of selected names throughout the code block. The
interpretation of selected names throughout the code block. The
following constructs bind names: formal parameters,
\keyword
{
import
}
following constructs bind names: formal parameters to functions,
statements, class and function definitions (these bind the class or
\keyword
{
import
}
statements, class and function definitions (these
function name), and targets that are identifiers if occurring in an
bind the class or function name in the defining block), and targets
assignment,
\keyword
{
for
}
loop header, or except clause header.
that are identifiers if occurring in an assignment,
\keyword
{
for
}
loop
header, or in the second position of an
\keyword
{
except
}
clause
header. Local names are searched only on the local namespace; global
names are searched only in the global and built-in namespace.
%
%
\footnote
{
If the code block contains
\keyword
{
exec
}
statements or the
construct ``
\samp
{
from
\ldots
import *
}
'', the semantics of local
names change: local name lookup first searches the local namespace,
then the global namespace and the built-in namespace.
}
A target occurring in a
\keyword
{
del
}
statement is also considered bound
A target occurring in a
\keyword
{
del
}
statement is also considered bound
for this purpose (though the actual semantics are to ``unbind'' the
for this purpose (though the actual semantics are to ``unbind'' the
name).
name).
When a global name is not found in the global name space, it is
When a global name is not found in the global namespace, it is
searched in the list of ``built-in'' names (which is actually the
searched in the built-in namespace (which is actually the global
global name space of the module
\module
{__
builtin
__}
). When a name is not
namespace of the module
\module
{__
builtin
__}
). The built-in namespace
found at all, the
\exception
{
NameError
}
exception is raised.
%
associated with the execution of a code block is actually found by
\footnote
{
If the code block contains
\keyword
{
exec
}
statements or the
looking up the name
\code
{__
builtins
__}
is its global namespace; this
construct
\samp
{
from
\ldots
import *
}
, the semantics of names not
should be a dictionary or a module (in the latter case its dictionary
explicitly mentioned in a
{
\tt
global
}
statement change subtly: name
is used). Normally, the
\code
{__
builtins
__}
namespace is the
lookup first searches the local name space, then the global one, then
dictionary of the built-in module
\module
{__
builtin
__}
(note: no `s');
the built-in one.
}
if it isn't, restricted execution mode is in effect. When a name is
not found at all, a
\exception
{
NameError
}
exception is raised.
%
\refbimodindex
{__
builtin
__}
\refbimodindex
{__
builtin
__}
\stindex
{
from
}
\stindex
{
from
}
\stindex
{
exec
}
\stindex
{
exec
}
\stindex
{
global
}
\stindex
{
global
}
\indexii
{
restricted
}{
execution
}
\withsubitem
{
(built-in exception)
}{
\ttindex
{
NameError
}}
\withsubitem
{
(built-in exception)
}{
\ttindex
{
NameError
}}
The following table lists the meaning of the local and global
name
The following table lists the meaning of the local and global
space for various types of code blocks. The name
space for a
namespace for various types of code blocks. The name
space for a
particular module is automatically created when the module is first
particular module is automatically created when the module is first
referenced. Note that in almost all cases, the global name space is
imported (i.e., when it is loaded). Note that in almost all cases,
the
name space of the containing module --- scopes in Python do not
the
global namespace is the namespace of the containing module ---
nest!
scopes in Python do not
nest!
\begin{center}
\begin{center}
\begin{tabular}
{
|l|l|l|l|
}
\begin{tabular}
{
|l|l|l|l|
}
\hline
\hline
Code block type
&
Global name
space
&
Local name
space
&
Notes
\\
Code block type
&
Global name
space
&
Local name
space
&
Notes
\\
\hline
\hline
Module
&
n.s. for this module
&
same as global
&
\\
Module
&
n.s. for this module
&
same as global
&
\\
Script
&
n.s. for
\module
{__
main
__}
&
same as global
&
\\
Script (file or command)
&
n.s. for
\module
{__
main
__}
&
same as global
&
(1)
\\
Interactive command
&
n.s. for
\module
{__
main
__}
&
same as global
&
\\
Interactive command
&
n.s. for
\module
{__
main
__}
&
same as global
&
\\
Class definition
&
global n.s. of containing block
&
new n.s.
&
\\
Class definition
&
global n.s. of containing block
&
new n.s.
&
\\
Function body
&
global n.s. of containing block
&
new n.s.
&
(2)
\\
Function body
&
global n.s. of containing block
&
new n.s.
&
(2)
\\
String passed to
\keyword
{
exec
}
statement
String passed to
\keyword
{
exec
}
statement
&
global n.s. of containing block
&
global n.s. of containing block
&
local n.s. of containing block
&
(
1
)
\\
&
local n.s. of containing block
&
(
2), (3
)
\\
String passed to
\function
{
eval()
}
String passed to
\function
{
eval()
}
&
global n.s. of caller
&
local n.s. of caller
&
(
1
)
\\
&
global n.s. of caller
&
local n.s. of caller
&
(
2), (3
)
\\
File read by
\function
{
execfile()
}
File read by
\function
{
execfile()
}
&
global n.s. of caller
&
local n.s. of caller
&
(
1
)
\\
&
global n.s. of caller
&
local n.s. of caller
&
(
2), (3
)
\\
Expression read by
\function
{
input()
}
Expression read by
\function
{
input()
}
&
global n.s. of caller
&
local n.s. of caller
&
\\
&
global n.s. of caller
&
local n.s. of caller
&
\\
\hline
\hline
...
@@ -117,25 +134,28 @@ Notes:
...
@@ -117,25 +134,28 @@ Notes:
\begin{description}
\begin{description}
\item
[n.s.]
means
{
\em
name
space
}
\item
[n.s.]
means
{
\em
namespace
}
\item
[(1)]
The global and local name space for these can be
\item
[(1)]
The main module for a script is always called
\module
{__
main
__}
; ``the filename don't enter into it.''
\item
[(2)]
The global and local namespace for these can be
overridden with optional extra arguments.
overridden with optional extra arguments.
\item
[(
2)]
The body of lambda forms (see section
\ref
{
lambda
}
) is
\item
[(
3)]
The
\keyword
{
exec
}
statement and the
\function
{
eval()
}
and
treated exactly the same as a (nested) function definition. Lambda
\function
{
execfile()
}
functions have optional arguments to override
forms have their own name space consisting of their formal arguments.
the global and local namespace. If only one namespace is specified,
\indexii
{
lambda
}{
form
}
it is used for both.
\end{description}
\end{description}
The built-in functions
\function
{
globals()
}
and
\function
{
locals()
}
returns a
The built-in functions
\function
{
globals()
}
and
\function
{
locals()
}
returns a
dictionary representing the current global and local name
space,
dictionary representing the current global and local namespace,
respectively. The effect of modifications to this dictionary on the
respectively. The effect of modifications to this dictionary on the
name
space are undefined.
%
namespace are undefined.
%
\footnote
{
The current implementations return the dictionary actually
\footnote
{
The current implementations return the dictionary actually
used to implement the name
space,
{
\em
except
}
for functions, where
used to implement the namespace,
{
\em
except
}
for functions, where
the optimizer may cause the local name
space to be implemented
the optimizer may cause the local namespace to be implemented
differently, and
\function
{
locals()
}
returns a read-only dictionary.
}
differently, and
\function
{
locals()
}
returns a read-only dictionary.
}
\section
{
Exceptions
}
\section
{
Exceptions
}
...
@@ -153,48 +173,39 @@ where the error occurred.
...
@@ -153,48 +173,39 @@ where the error occurred.
\index
{
errors
}
\index
{
errors
}
\index
{
error handling
}
\index
{
error handling
}
The Python interpreter raises an exception when it detects a
n
run-time
The Python interpreter raises an exception when it detects a run-time
error (such as division by zero). A Python program can also
error (such as division by zero). A Python program can also
explicitly raise an exception with the
\keyword
{
raise
}
statement.
explicitly raise an exception with the
\keyword
{
raise
}
statement.
Exception handlers are specified with the
\keyword
{
try
}
...
\keyword
{
except
}
Exception handlers are specified with the
\keyword
{
try
}
...
\keyword
{
except
}
statement.
statement. The
\keyword
{
try
}
...
\keyword
{
finally
}
statement
specifies cleanup code which does not handle the exception, but is
executed whether an exception occurred or not in the preceding code.
Python uses the ``termination'' model of error handling: an exception
Python uses the ``termination'' model of error handling: an exception
handler can find out what happened and continue execution at an outer
handler can find out what happened and continue execution at an outer
level, but it cannot repair the cause of the error and retry the
level, but it cannot repair the cause of the error and retry the
failing operation (except by re-entering the
the
offending piece of
failing operation (except by re-entering the offending piece of
code from the top).
code from the top).
When an exception is not handled at all, the interpreter terminates
When an exception is not handled at all, the interpreter terminates
execution of the program, or returns to its interactive main loop.
execution of the program, or returns to its interactive main loop. In
either case, it prints a stack backtrace, except when the exception is
Exceptions are identified by string objects or class instances. Two
\exception
{
SystemExit
}
.
\ttindex
{
SystemExit
}
different string objects with the same value identify different
exceptions. An exception can be raised with a class instance. Such
Exceptions are identified by string objects or class instances.
exceptions are caught by specifying an except clause that has the
Selection of a matching except clause is based on object identity
class name (or a base class) as the condition.
(i.e., two different string objects with the same value represent
different exceptions!) For string exceptions, the
\keyword
{
except
}
clause must reference the same string object. For class exceptions,
the
\keyword
{
except
}
clause must reference the same class or a base
class of it.
When an exception is raised, an object (maybe
\code
{
None
}
) is passed
When an exception is raised, an object (maybe
\code
{
None
}
) is passed
as the exception's ``parameter''; this object does not affect the
as the exception's ``parameter'' or ``value''; this object does not
selection of an exception handler, but is passed to the selected
affect the selection of an exception handler, but is passed to the
exception handler as additional information. For exceptions raised
selected exception handler as additional information. For class
with a class instance, the instance is passed as the ``parameter''.
exceptions, this object must be an instance of the exception class
being raised.
For example:
\begin{verbatim}
>>> class Error:
... def
__
init
__
(self, msg): self.msg = msg
...
>>> class SpecificError(Error): pass
...
>>> try:
... raise SpecificError('broken')
... except Error, obj:
... print obj.msg
...
broken
\end{verbatim}
See also the description of the
\keyword
{
try
}
and
\keyword
{
raise
}
See also the description of the
\keyword
{
try
}
and
\keyword
{
raise
}
statements.
statements
in chapter 7
.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment