Fred Drake | 431f0ce | 1999-05-13 18:38:11 +0000 | [diff] [blame] | 1 | \chapter{Execution model \label{execmodel}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 2 | \index{execution model} |
| 3 | |
Fred Drake | 431f0ce | 1999-05-13 18:38:11 +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} |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 6 | \index{namespace} |
Fred Drake | 431f0ce | 1999-05-13 18:38:11 +0000 | [diff] [blame] | 7 | \indexii{execution}{frame} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 8 | |
Fred Drake | 431f0ce | 1999-05-13 18:38:11 +0000 | [diff] [blame] | 9 | A \dfn{code block}\indexii{code}{block} is a piece |
| 10 | of Python program text that can be executed as a unit, such as a |
| 11 | module, a class definition or a function body. Some code blocks (like |
| 12 | modules) are normally executed only once, others (like function |
| 13 | bodies) may be executed many times. Code blocks may textually contain |
| 14 | other code blocks. Code blocks may invoke other code blocks (that may |
| 15 | or may not be textually contained in them) as part of their execution, |
| 16 | e.g., by invoking (calling) a function. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 17 | |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 18 | 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] | 19 | 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] | 20 | command typed interactively is a separate code block; a script file (a |
| 21 | file given as standard input to the interpreter or specified on the |
| 22 | interpreter command line the first argument) is a code block; a script |
| 23 | command (a command specified on the interpreter command line with the |
Fred Drake | 431f0ce | 1999-05-13 18:38:11 +0000 | [diff] [blame] | 24 | `\strong{-c}' option) is a code block. The file read by the built-in |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 25 | function \function{execfile()} is a code block. The string argument |
| 26 | passed to the built-in function \function{eval()} and to the |
Fred Drake | 667c9e4 | 2001-02-02 02:43:18 +0000 | [diff] [blame^] | 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. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 30 | |
Fred Drake | a1cce71 | 1998-07-24 22:12:32 +0000 | [diff] [blame] | 31 | A code block is executed in an execution frame. An \dfn{execution |
Fred Drake | 431f0ce | 1999-05-13 18:38:11 +0000 | [diff] [blame] | 32 | frame}\indexii{execution}{frame} contains some administrative |
| 33 | information (used for debugging), determines where and how execution |
| 34 | continues after the code block's execution has completed, and (perhaps |
Jeremy Hylton | 0eb1070 | 2001-02-01 03:50:59 +0000 | [diff] [blame] | 35 | most importantly) defines the environment in which names are resolved. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 36 | |
Jeremy Hylton | 0eb1070 | 2001-02-01 03:50:59 +0000 | [diff] [blame] | 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 |
Fred Drake | 431f0ce | 1999-05-13 18:38:11 +0000 | [diff] [blame] | 50 | \dfn{unbinding}\indexii{unbinding}{name}. Namespaces are functionally |
| 51 | equivalent to dictionaries (and often implemented as dictionaries). |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 52 | |
Jeremy Hylton | 0eb1070 | 2001-02-01 03:50:59 +0000 | [diff] [blame] | 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. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 86 | |
| 87 | Whether a name is local or global in a code block is determined by |
| 88 | static inspection of the source text for the code block: in the |
Fred Drake | 667c9e4 | 2001-02-02 02:43:18 +0000 | [diff] [blame^] | 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. |
Jeremy Hylton | 0eb1070 | 2001-02-01 03:50:59 +0000 | [diff] [blame] | 94 | |
| 95 | The following constructs bind names: formal parameters to functions, |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 96 | \keyword{import} statements, class and function definitions (these |
| 97 | bind the class or function name in the defining block), and targets |
| 98 | that are identifiers if occurring in an assignment, \keyword{for} loop |
| 99 | header, or in the second position of an \keyword{except} clause |
Jeremy Hylton | 0eb1070 | 2001-02-01 03:50:59 +0000 | [diff] [blame] | 100 | header. The \keyword{import} statement of the form ``\samp{from |
Fred Drake | 667c9e4 | 2001-02-02 02:43:18 +0000 | [diff] [blame^] | 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. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 104 | |
| 105 | A target occurring in a \keyword{del} statement is also considered bound |
Jeremy Hylton | 0eb1070 | 2001-02-01 03:50:59 +0000 | [diff] [blame] | 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}. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 109 | |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 110 | When a global name is not found in the global namespace, it is |
| 111 | searched in the built-in namespace (which is actually the global |
Jeremy Hylton | 0eb1070 | 2001-02-01 03:50:59 +0000 | [diff] [blame] | 112 | namespace of the module \module{__builtin__}\refbimodindex{__builtin__}). |
| 113 | The built-in namespace associated with the execution of a code block |
Fred Drake | 667c9e4 | 2001-02-02 02:43:18 +0000 | [diff] [blame^] | 114 | is actually found by looking up the name \code{__builtins__} in its |
Jeremy Hylton | 0eb1070 | 2001-02-01 03:50:59 +0000 | [diff] [blame] | 115 | global namespace; this should be a dictionary or a module (in the |
Fred Drake | 667c9e4 | 2001-02-02 02:43:18 +0000 | [diff] [blame^] | 116 | latter case the module's dictionary is used). Normally, the |
Jeremy Hylton | 0eb1070 | 2001-02-01 03:50:59 +0000 | [diff] [blame] | 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. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 120 | |
Jeremy Hylton | 0eb1070 | 2001-02-01 03:50:59 +0000 | [diff] [blame] | 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__}. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 124 | |
Jeremy Hylton | 0eb1070 | 2001-02-01 03:50:59 +0000 | [diff] [blame] | 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 |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 134 | \function{execfile()} functions have optional arguments to override |
| 135 | the global and local namespace. If only one namespace is specified, |
| 136 | it is used for both. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 137 | |
Jeremy Hylton | 0eb1070 | 2001-02-01 03:50:59 +0000 | [diff] [blame] | 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{ |
Fred Drake | 431f0ce | 1999-05-13 18:38:11 +0000 | [diff] [blame] | 142 | The current implementations return the dictionary actually used to |
| 143 | implement the namespace, \emph{except} for functions, where the |
| 144 | optimizer may cause the local namespace to be implemented |
| 145 | differently, and \function{locals()} returns a read-only |
| 146 | dictionary.} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 147 | |
Fred Drake | 431f0ce | 1999-05-13 18:38:11 +0000 | [diff] [blame] | 148 | |
| 149 | \section{Exceptions \label{exceptions}} |
| 150 | \index{exception} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 151 | |
| 152 | Exceptions are a means of breaking out of the normal flow of control |
| 153 | of a code block in order to handle errors or other exceptional |
Fred Drake | 431f0ce | 1999-05-13 18:38:11 +0000 | [diff] [blame] | 154 | conditions. An exception is |
| 155 | \emph{raised}\index{raise an exception} at the point where the error |
| 156 | is detected; it may be \emph{handled}\index{handle an exception} by |
| 157 | the surrounding code block or by any code block that directly or |
| 158 | indirectly invoked the code block where the error occurred. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 159 | \index{exception handler} |
| 160 | \index{errors} |
| 161 | \index{error handling} |
| 162 | |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 163 | The Python interpreter raises an exception when it detects a run-time |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 164 | error (such as division by zero). A Python program can also |
| 165 | explicitly raise an exception with the \keyword{raise} statement. |
| 166 | Exception handlers are specified with the \keyword{try} ... \keyword{except} |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 167 | statement. The \keyword{try} ... \keyword{finally} statement |
| 168 | specifies cleanup code which does not handle the exception, but is |
| 169 | executed whether an exception occurred or not in the preceding code. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 170 | |
Fred Drake | e15956b | 2000-04-03 04:51:13 +0000 | [diff] [blame] | 171 | Python uses the ``termination'' \index{termination model}model of |
| 172 | error handling: an exception handler can find out what happened and |
| 173 | continue execution at an outer level, but it cannot repair the cause |
| 174 | of the error and retry the failing operation (except by re-entering |
| 175 | the offending piece of code from the top). |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 176 | |
| 177 | When an exception is not handled at all, the interpreter terminates |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 178 | execution of the program, or returns to its interactive main loop. In |
| 179 | either case, it prints a stack backtrace, except when the exception is |
Fred Drake | 431f0ce | 1999-05-13 18:38:11 +0000 | [diff] [blame] | 180 | \exception{SystemExit}\withsubitem{(built-in |
| 181 | exception)}{\ttindex{SystemExit}}. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 182 | |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 183 | Exceptions are identified by string objects or class instances. |
| 184 | Selection of a matching except clause is based on object identity |
| 185 | (i.e., two different string objects with the same value represent |
| 186 | different exceptions!) For string exceptions, the \keyword{except} |
| 187 | clause must reference the same string object. For class exceptions, |
| 188 | the \keyword{except} clause must reference the same class or a base |
| 189 | class of it. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 190 | |
| 191 | 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] | 192 | as the exception's ``parameter'' or ``value''; this object does not |
| 193 | affect the selection of an exception handler, but is passed to the |
| 194 | selected exception handler as additional information. For class |
| 195 | exceptions, this object must be an instance of the exception class |
| 196 | being raised. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 197 | |
Fred Drake | e15956b | 2000-04-03 04:51:13 +0000 | [diff] [blame] | 198 | See also the description of the \keyword{try} statement in section |
| 199 | \ref{try} and \keyword{raise} statement in section \ref{raise}. |