Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1 | \chapter{Execution model} |
| 2 | \index{execution model} |
| 3 | |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 4 | \section{Code blocks, execution frames, and namespaces} \label{execframes} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 5 | \index{code block} |
| 6 | \indexii{execution}{frame} |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 7 | \index{namespace} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 8 | |
| 9 | A {\em code block} is a piece of Python program text that can be |
| 10 | executed as a unit, such as a module, a class definition or a function |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 11 | body. Some code blocks (like modules) are normally executed only once, others |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 12 | (like function bodies) may be executed many times. Code blocks may |
| 13 | textually contain other code blocks. Code blocks may invoke other |
| 14 | code blocks (that may or may not be textually contained in them) as |
Guido van Rossum | 7c0240f | 1998-07-24 15:36:43 +0000 | [diff] [blame^] | 15 | part of their execution, e.g., by invoking (calling) a function. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 16 | \index{code block} |
| 17 | \indexii{code}{block} |
| 18 | |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 19 | The following are code blocks: A module is a code block. A function |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 20 | body is a code block. A class definition is a code block. Each |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 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 | `\code{-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. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 31 | |
| 32 | A code block is executed in an execution frame. An {\em execution |
| 33 | frame} contains some administrative information (used for debugging), |
| 34 | determines where and how execution continues after the code block's |
| 35 | execution has completed, and (perhaps most importantly) defines two |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 36 | namespaces, the local and the global namespace, that affect |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 37 | execution of the code block. |
| 38 | \indexii{execution}{frame} |
| 39 | |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 40 | A {\em namespace} is a mapping from names (identifiers) to objects. |
| 41 | A particular namespace may be referenced by more than one execution |
| 42 | frame, and from other places as well. Adding a name to a namespace |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 43 | is called {\em binding} a name (to an object); changing the mapping of |
| 44 | a name is called {\em rebinding}; removing a name is {\em unbinding}. |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 45 | Namespaces are functionally equivalent to dictionaries (and often |
| 46 | implemented as dictionaries). |
| 47 | \index{namespace} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 48 | \indexii{binding}{name} |
| 49 | \indexii{rebinding}{name} |
| 50 | \indexii{unbinding}{name} |
| 51 | |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 52 | The {\em local namespace} of an execution frame determines the default |
| 53 | place where names are defined and searched. The {\em global |
| 54 | namespace} determines the place where names listed in \keyword{global} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 55 | statements are defined and searched, and where names that are not |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 56 | bound anywhere in the current code block are searched. |
| 57 | \indexii{local}{namespace} |
| 58 | \indexii{global}{namespace} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 59 | \stindex{global} |
| 60 | |
| 61 | Whether a name is local or global in a code block is determined by |
| 62 | static inspection of the source text for the code block: in the |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 63 | absence of \keyword{global} statements, a name that is bound anywhere |
| 64 | in the code block is local in the entire code block; all other names |
| 65 | are considered global. The \keyword{global} statement forces global |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 66 | interpretation of selected names throughout the code block. The |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 67 | following constructs bind names: formal parameters to functions, |
| 68 | \keyword{import} statements, class and function definitions (these |
| 69 | bind the class or function name in the defining block), and targets |
| 70 | that are identifiers if occurring in an assignment, \keyword{for} loop |
| 71 | header, or in the second position of an \keyword{except} clause |
| 72 | header. Local names are searched only on the local namespace; global |
| 73 | names are searched only in the global and built-in namespace.% |
| 74 | % |
| 75 | \footnote{If the code block contains \keyword{exec} statements or the |
| 76 | construct ``\samp{from \ldots import *}'', the semantics of local |
| 77 | names change: local name lookup first searches the local namespace, |
| 78 | then the global namespace and the built-in namespace.} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 79 | |
| 80 | A target occurring in a \keyword{del} statement is also considered bound |
| 81 | for this purpose (though the actual semantics are to ``unbind'' the |
| 82 | name). |
| 83 | |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 84 | When a global name is not found in the global namespace, it is |
| 85 | searched in the built-in namespace (which is actually the global |
| 86 | namespace of the module \module{__builtin__}). The built-in namespace |
| 87 | associated with the execution of a code block is actually found by |
| 88 | looking up the name \code{__builtins__} is its global namespace; this |
| 89 | should be a dictionary or a module (in the latter case its dictionary |
| 90 | is used). Normally, the \code{__builtins__} namespace is the |
| 91 | dictionary of the built-in module \module{__builtin__} (note: no `s'); |
| 92 | if it isn't, restricted execution mode is in effect. When a name is |
| 93 | not found at all, a \exception{NameError} exception is raised.% |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 94 | \refbimodindex{__builtin__} |
| 95 | \stindex{from} |
| 96 | \stindex{exec} |
| 97 | \stindex{global} |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 98 | \indexii{restricted}{execution} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 99 | \withsubitem{(built-in exception)}{\ttindex{NameError}} |
| 100 | |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 101 | The following table lists the meaning of the local and global |
| 102 | namespace for various types of code blocks. The namespace for a |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 103 | particular module is automatically created when the module is first |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 104 | imported (i.e., when it is loaded). Note that in almost all cases, |
| 105 | the global namespace is the namespace of the containing module --- |
| 106 | scopes in Python do not nest! |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 107 | |
| 108 | \begin{center} |
| 109 | \begin{tabular}{|l|l|l|l|} |
| 110 | \hline |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 111 | Code block type & Global namespace & Local namespace & Notes \\ |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 112 | \hline |
| 113 | Module & n.s. for this module & same as global & \\ |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 114 | Script (file or command) & n.s. for \module{__main__} & same as global |
| 115 | & (1) \\ |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 116 | Interactive command & n.s. for \module{__main__} & same as global & \\ |
| 117 | Class definition & global n.s. of containing block & new n.s. & \\ |
| 118 | Function body & global n.s. of containing block & new n.s. & (2) \\ |
| 119 | String passed to \keyword{exec} statement |
| 120 | & global n.s. of containing block |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 121 | & local n.s. of containing block & (2), (3) \\ |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 122 | String passed to \function{eval()} |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 123 | & global n.s. of caller & local n.s. of caller & (2), (3) \\ |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 124 | File read by \function{execfile()} |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 125 | & global n.s. of caller & local n.s. of caller & (2), (3) \\ |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 126 | Expression read by \function{input()} |
| 127 | & global n.s. of caller & local n.s. of caller & \\ |
| 128 | \hline |
| 129 | \end{tabular} |
| 130 | \end{center} |
| 131 | \refbimodindex{__main__} |
| 132 | |
| 133 | Notes: |
| 134 | |
| 135 | \begin{description} |
| 136 | |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 137 | \item[n.s.] means {\em namespace} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 138 | |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 139 | \item[(1)] The main module for a script is always called |
| 140 | \module{__main__}; ``the filename don't enter into it.'' |
| 141 | |
| 142 | \item[(2)] The global and local namespace for these can be |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 143 | overridden with optional extra arguments. |
| 144 | |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 145 | \item[(3)] The \keyword{exec} statement and the \function{eval()} and |
| 146 | \function{execfile()} functions have optional arguments to override |
| 147 | the global and local namespace. If only one namespace is specified, |
| 148 | it is used for both. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 149 | |
| 150 | \end{description} |
| 151 | |
| 152 | The built-in functions \function{globals()} and \function{locals()} returns a |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 153 | dictionary representing the current global and local namespace, |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 154 | respectively. The effect of modifications to this dictionary on the |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 155 | namespace are undefined.% |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 156 | \footnote{The current implementations return the dictionary actually |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 157 | used to implement the namespace, {\em except} for functions, where |
| 158 | the optimizer may cause the local namespace to be implemented |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 159 | differently, and \function{locals()} returns a read-only dictionary.} |
| 160 | |
| 161 | \section{Exceptions} |
| 162 | |
| 163 | Exceptions are a means of breaking out of the normal flow of control |
| 164 | of a code block in order to handle errors or other exceptional |
| 165 | conditions. An exception is {\em raised} at the point where the error |
| 166 | is detected; it may be {\em handled} by the surrounding code block or |
| 167 | by any code block that directly or indirectly invoked the code block |
| 168 | where the error occurred. |
| 169 | \index{exception} |
| 170 | \index{raise an exception} |
| 171 | \index{handle an exception} |
| 172 | \index{exception handler} |
| 173 | \index{errors} |
| 174 | \index{error handling} |
| 175 | |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 176 | The Python interpreter raises an exception when it detects a run-time |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 177 | error (such as division by zero). A Python program can also |
| 178 | explicitly raise an exception with the \keyword{raise} statement. |
| 179 | Exception handlers are specified with the \keyword{try} ... \keyword{except} |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 180 | statement. The \keyword{try} ... \keyword{finally} statement |
| 181 | specifies cleanup code which does not handle the exception, but is |
| 182 | executed whether an exception occurred or not in the preceding code. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 183 | |
| 184 | Python uses the ``termination'' model of error handling: an exception |
| 185 | handler can find out what happened and continue execution at an outer |
| 186 | level, but it cannot repair the cause of the error and retry the |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 187 | failing operation (except by re-entering the offending piece of |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 188 | code from the top). |
| 189 | |
| 190 | When an exception is not handled at all, the interpreter terminates |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 191 | execution of the program, or returns to its interactive main loop. In |
| 192 | either case, it prints a stack backtrace, except when the exception is |
| 193 | \exception{SystemExit}.\ttindex{SystemExit} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 194 | |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 195 | Exceptions are identified by string objects or class instances. |
| 196 | Selection of a matching except clause is based on object identity |
| 197 | (i.e., two different string objects with the same value represent |
| 198 | different exceptions!) For string exceptions, the \keyword{except} |
| 199 | clause must reference the same string object. For class exceptions, |
| 200 | the \keyword{except} clause must reference the same class or a base |
| 201 | class of it. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 202 | |
| 203 | When an exception is raised, an object (maybe \code{None}) is passed |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 204 | as the exception's ``parameter'' or ``value''; this object does not |
| 205 | affect the selection of an exception handler, but is passed to the |
| 206 | selected exception handler as additional information. For class |
| 207 | exceptions, this object must be an instance of the exception class |
| 208 | being raised. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 209 | |
| 210 | See also the description of the \keyword{try} and \keyword{raise} |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 211 | statements in chapter 7. |