Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1 | \chapter{Compound statements} |
| 2 | \indexii{compound}{statement} |
| 3 | |
| 4 | Compound statements contain (groups of) other statements; they affect |
| 5 | or control the execution of those other statements in some way. In |
| 6 | general, compound statements span multiple lines, although in simple |
| 7 | incarnations a whole compound statement may be contained in one line. |
| 8 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 9 | The \keyword{if}, \keyword{while} and \keyword{for} statements implement |
| 10 | traditional control flow constructs. \keyword{try} specifies exception |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 11 | handlers and/or cleanup code for a group of statements. Function and |
| 12 | class definitions are also syntactically compound statements. |
| 13 | |
| 14 | Compound statements consist of one or more `clauses'. A clause |
| 15 | consists of a header and a `suite'. The clause headers of a |
| 16 | particular compound statement are all at the same indentation level. |
| 17 | Each clause header begins with a uniquely identifying keyword and ends |
| 18 | with a colon. A suite is a group of statements controlled by a |
| 19 | clause. A suite can be one or more semicolon-separated simple |
| 20 | statements on the same line as the header, following the header's |
| 21 | colon, or it can be one or more indented statements on subsequent |
| 22 | lines. Only the latter form of suite can contain nested compound |
| 23 | statements; the following is illegal, mostly because it wouldn't be |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 24 | clear to which \keyword{if} clause a following \keyword{else} clause would |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 25 | belong: |
| 26 | \index{clause} |
| 27 | \index{suite} |
| 28 | |
| 29 | \begin{verbatim} |
| 30 | if test1: if test2: print x |
| 31 | \end{verbatim} |
| 32 | |
| 33 | Also note that the semicolon binds tighter than the colon in this |
| 34 | context, so that in the following example, either all or none of the |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 35 | \keyword{print} statements are executed: |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 36 | |
| 37 | \begin{verbatim} |
| 38 | if x < y < z: print x; print y; print z |
| 39 | \end{verbatim} |
| 40 | |
| 41 | Summarizing: |
| 42 | |
| 43 | \begin{verbatim} |
| 44 | compound_stmt: if_stmt | while_stmt | for_stmt |
| 45 | | try_stmt | funcdef | classdef |
| 46 | suite: stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT |
| 47 | statement: stmt_list NEWLINE | compound_stmt |
| 48 | stmt_list: simple_stmt (";" simple_stmt)* [";"] |
| 49 | \end{verbatim} |
| 50 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 51 | Note that statements always end in a \code{NEWLINE} possibly followed |
| 52 | by a \code{DEDENT}. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 53 | \index{NEWLINE token} |
| 54 | \index{DEDENT token} |
| 55 | |
| 56 | Also note that optional continuation clauses always begin with a |
| 57 | keyword that cannot start a statement, thus there are no ambiguities |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 58 | (the `dangling \keyword{else}' problem is solved in Python by requiring |
| 59 | nested \keyword{if} statements to be indented). |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 60 | \indexii{dangling}{else} |
| 61 | |
| 62 | The formatting of the grammar rules in the following sections places |
| 63 | each clause on a separate line for clarity. |
| 64 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 65 | \section{The \keyword{if} statement} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 66 | \stindex{if} |
| 67 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 68 | The \keyword{if} statement is used for conditional execution: |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 69 | |
| 70 | \begin{verbatim} |
| 71 | if_stmt: "if" condition ":" suite |
| 72 | ("elif" condition ":" suite)* |
| 73 | ["else" ":" suite] |
| 74 | \end{verbatim} |
| 75 | |
| 76 | It selects exactly one of the suites by evaluating the conditions one |
| 77 | by one until one is found to be true (see section \ref{Booleans} for |
| 78 | the definition of true and false); then that suite is executed (and no |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 79 | other part of the \keyword{if} statement is executed or evaluated). If |
| 80 | all conditions are false, the suite of the \keyword{else} clause, if |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 81 | present, is executed. |
| 82 | \kwindex{elif} |
| 83 | \kwindex{else} |
| 84 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 85 | \section{The \keyword{while} statement} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 86 | \stindex{while} |
| 87 | \indexii{loop}{statement} |
| 88 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 89 | The \keyword{while} statement is used for repeated execution as long as a |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 90 | condition is true: |
| 91 | |
| 92 | \begin{verbatim} |
| 93 | while_stmt: "while" condition ":" suite |
| 94 | ["else" ":" suite] |
| 95 | \end{verbatim} |
| 96 | |
| 97 | This repeatedly tests the condition and, if it is true, executes the |
| 98 | first suite; if the condition is false (which may be the first time it |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 99 | is tested) the suite of the \keyword{else} clause, if present, is |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 100 | executed and the loop terminates. |
| 101 | \kwindex{else} |
| 102 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 103 | A \keyword{break} statement executed in the first suite terminates the |
| 104 | loop without executing the \keyword{else} clause's suite. A |
| 105 | \keyword{continue} statement executed in the first suite skips the rest |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 106 | of the suite and goes back to testing the condition. |
| 107 | \stindex{break} |
| 108 | \stindex{continue} |
| 109 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 110 | \section{The \keyword{for} statement} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 111 | \stindex{for} |
| 112 | \indexii{loop}{statement} |
| 113 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 114 | The \keyword{for} statement is used to iterate over the elements of a |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 115 | sequence (string, tuple or list): |
| 116 | \obindex{sequence} |
| 117 | |
| 118 | \begin{verbatim} |
| 119 | for_stmt: "for" target_list "in" condition_list ":" suite |
| 120 | ["else" ":" suite] |
| 121 | \end{verbatim} |
| 122 | |
| 123 | The condition list is evaluated once; it should yield a sequence. The |
| 124 | suite is then executed once for each item in the sequence, in the |
| 125 | order of ascending indices. Each item in turn is assigned to the |
| 126 | target list using the standard rules for assignments, and then the |
| 127 | suite is executed. When the items are exhausted (which is immediately |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 128 | when the sequence is empty), the suite in the \keyword{else} clause, if |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 129 | present, is executed, and the loop terminates. |
| 130 | \kwindex{in} |
| 131 | \kwindex{else} |
| 132 | \indexii{target}{list} |
| 133 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 134 | A \keyword{break} statement executed in the first suite terminates the |
| 135 | loop without executing the \keyword{else} clause's suite. A |
| 136 | \keyword{continue} statement executed in the first suite skips the rest |
| 137 | of the suite and continues with the next item, or with the \keyword{else} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 138 | clause if there was no next item. |
| 139 | \stindex{break} |
| 140 | \stindex{continue} |
| 141 | |
| 142 | The suite may assign to the variable(s) in the target list; this does |
| 143 | not affect the next item assigned to it. |
| 144 | |
| 145 | The target list is not deleted when the loop is finished, but if the |
| 146 | sequence is empty, it will not have been assigned to at all by the |
| 147 | loop. |
| 148 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 149 | Hint: the built-in function \function{range()} returns a sequence of |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 150 | integers suitable to emulate the effect of Pascal's |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 151 | \code{for i := a to b do}; |
| 152 | e.g. \code{range(3)} returns the list \code{[0, 1, 2]}. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 153 | \bifuncindex{range} |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 154 | \indexii{Pascal}{language} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 155 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 156 | \strong{Warning:} There is a subtlety when the sequence is being modified |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 157 | by the loop (this can only occur for mutable sequences, i.e. lists). |
| 158 | An internal counter is used to keep track of which item is used next, |
| 159 | and this is incremented on each iteration. When this counter has |
| 160 | reached the length of the sequence the loop terminates. This means that |
| 161 | if the suite deletes the current (or a previous) item from the |
| 162 | sequence, the next item will be skipped (since it gets the index of |
| 163 | the current item which has already been treated). Likewise, if the |
| 164 | suite inserts an item in the sequence before the current item, the |
| 165 | current item will be treated again the next time through the loop. |
| 166 | This can lead to nasty bugs that can be avoided by making a temporary |
| 167 | copy using a slice of the whole sequence, e.g. |
| 168 | \index{loop!over mutable sequence} |
| 169 | \index{mutable sequence!loop over} |
| 170 | |
| 171 | \begin{verbatim} |
| 172 | for x in a[:]: |
| 173 | if x < 0: a.remove(x) |
| 174 | \end{verbatim} |
| 175 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 176 | \section{The \keyword{try} statement} \label{try} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 177 | \stindex{try} |
| 178 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 179 | The \keyword{try} statement specifies exception handlers and/or cleanup |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 180 | code for a group of statements: |
| 181 | |
| 182 | \begin{verbatim} |
| 183 | try_stmt: try_exc_stmt | try_fin_stmt |
| 184 | try_exc_stmt: "try" ":" suite |
| 185 | ("except" [condition ["," target]] ":" suite)+ |
| 186 | ["else" ":" suite] |
| 187 | try_fin_stmt: "try" ":" suite |
| 188 | "finally" ":" suite |
| 189 | \end{verbatim} |
| 190 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 191 | There are two forms of \keyword{try} statement: |
| 192 | \keyword{try}...\keyword{except} and |
| 193 | \keyword{try}...\keyword{finally}. These forms cannot be mixed. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 194 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 195 | The \keyword{try}...\keyword{except} form specifies one or more |
| 196 | exception handlers |
| 197 | (the \keyword{except} clauses). When no exception occurs in the |
| 198 | \keyword{try} clause, no exception handler is executed. When an |
| 199 | exception occurs in the \keyword{try} suite, a search for an exception |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 200 | handler is started. This inspects the except clauses in turn until |
| 201 | one is found that matches the exception. A condition-less except |
| 202 | clause, if present, must be last; it matches any exception. For an |
| 203 | except clause with a condition, that condition is evaluated, and the |
| 204 | clause matches the exception if the resulting object is ``compatible'' |
| 205 | with the exception. An object is compatible with an exception if it |
| 206 | is either the object that identifies the exception, or (for exceptions |
| 207 | that are classes) it is a base class of the exception, or it is a |
| 208 | tuple containing an item that is compatible with the exception. Note |
| 209 | that the object identities must match, i.e. it must be the same |
| 210 | object, not just an object with the same value. |
| 211 | \kwindex{except} |
| 212 | |
| 213 | If no except clause matches the exception, the search for an exception |
| 214 | handler continues in the surrounding code and on the invocation stack. |
| 215 | |
| 216 | If the evaluation of a condition in the header of an except clause |
| 217 | raises an exception, the original search for a handler is cancelled |
| 218 | and a search starts for the new exception in the surrounding code and |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 219 | on the call stack (it is treated as if the entire \keyword{try} statement |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 220 | raised the exception). |
| 221 | |
| 222 | When a matching except clause is found, the exception's parameter is |
| 223 | assigned to the target specified in that except clause, if present, |
| 224 | and the except clause's suite is executed. When the end of this suite |
| 225 | is reached, execution continues normally after the entire try |
| 226 | statement. (This means that if two nested handlers exist for the same |
| 227 | exception, and the exception occurs in the try clause of the inner |
| 228 | handler, the outer handler will not handle the exception.) |
| 229 | |
| 230 | Before an except clause's suite is executed, details about the |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 231 | exception are assigned to three variables in the \module{sys} module: |
| 232 | \code{sys.exc_type} receives the object identifying the exception; |
| 233 | \code{sys.exc_value} receives the exception's parameter; |
| 234 | \code{sys.exc_traceback} receives a traceback object (see section |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 235 | \ref{traceback}) identifying the point in the program where the |
| 236 | exception occurred. |
| 237 | \refbimodindex{sys} |
| 238 | \ttindex{exc_type} |
| 239 | \ttindex{exc_value} |
| 240 | \ttindex{exc_traceback} |
| 241 | \obindex{traceback} |
| 242 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 243 | The optional \keyword{else} clause is executed when no exception occurs |
| 244 | in the \keyword{try} clause. Exceptions in the \keyword{else} clause are |
| 245 | not handled by the preceding \keyword{except} clauses. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 246 | \kwindex{else} |
| 247 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 248 | The \keyword{try}...\keyword{finally} form specifies a `cleanup' handler. The |
| 249 | \keyword{try} clause is executed. When no exception occurs, the |
| 250 | \keyword{finally} clause is executed. When an exception occurs in the |
| 251 | \keyword{try} clause, the exception is temporarily saved, the |
| 252 | \keyword{finally} clause is executed, and then the saved exception is |
| 253 | re-raised. If the \keyword{finally} clause raises another exception or |
| 254 | executes a \keyword{return}, \keyword{break} or \keyword{continue} statement, |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 255 | the saved exception is lost. |
| 256 | \kwindex{finally} |
| 257 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 258 | When a \keyword{return} or \keyword{break} statement is executed in the |
| 259 | \keyword{try} suite of a \keyword{try}...\keyword{finally} statement, the |
| 260 | \keyword{finally} clause is also executed `on the way out'. A |
| 261 | \keyword{continue} statement is illegal in the \keyword{try} clause. (The |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 262 | reason is a problem with the current implementation --- this |
| 263 | restriction may be lifted in the future). |
| 264 | \stindex{return} |
| 265 | \stindex{break} |
| 266 | \stindex{continue} |
| 267 | |
| 268 | \section{Function definitions} \label{function} |
| 269 | \indexii{function}{definition} |
| 270 | |
| 271 | A function definition defines a user-defined function object (see |
| 272 | section \ref{types}):\footnote{The new syntax to receive arbitrary |
| 273 | keyword arguments is not yet documented in this manual. See chapter |
| 274 | 12 of the Tutorial.} |
| 275 | \obindex{user-defined function} |
| 276 | \obindex{function} |
| 277 | |
| 278 | \begin{verbatim} |
| 279 | funcdef: "def" funcname "(" [parameter_list] ")" ":" suite |
| 280 | parameter_list: (defparameter ",")* ("*" identifier [, "**" identifier] |
| 281 | | "**" identifier |
| 282 | | defparameter [","]) |
| 283 | defparameter: parameter ["=" condition] |
| 284 | sublist: parameter ("," parameter)* [","] |
| 285 | parameter: identifier | "(" sublist ")" |
| 286 | funcname: identifier |
| 287 | \end{verbatim} |
| 288 | |
| 289 | A function definition is an executable statement. Its execution binds |
| 290 | the function name in the current local name space to a function object |
| 291 | (a wrapper around the executable code for the function). This |
| 292 | function object contains a reference to the current global name space |
| 293 | as the global name space to be used when the function is called. |
| 294 | \indexii{function}{name} |
| 295 | \indexii{name}{binding} |
| 296 | |
| 297 | The function definition does not execute the function body; this gets |
| 298 | executed only when the function is called. |
| 299 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 300 | When one or more top-level parameters have the form \var{parameter \code{=} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 301 | condition}, the function is said to have ``default parameter values''. |
| 302 | Default parameter values are evaluated when the function definition is |
| 303 | executed. For a parameter with a default value, the correponding |
| 304 | argument may be omitted from a call, in which case the parameter's |
| 305 | default value is substituted. If a parameter has a default value, all |
| 306 | following parameters must also have a default value --- this is a |
| 307 | syntactic restriction that is not expressed by the grammar.% |
| 308 | \footnote{Currently this is not checked; instead, |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 309 | \code{def f(a=1, b)} is interpreted as \code{def f(a=1, b=None)}.} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 310 | \indexiii{default}{parameter}{value} |
| 311 | |
| 312 | Function call semantics are described in section \ref{calls}. When a |
| 313 | user-defined function is called, first missing arguments for which a |
| 314 | default value exists are supplied; then the arguments (a.k.a. actual |
| 315 | parameters) are bound to the (formal) parameters, as follows: |
| 316 | \indexii{function}{call} |
| 317 | \indexiii{user-defined}{function}{call} |
| 318 | \index{parameter} |
| 319 | \index{argument} |
| 320 | \indexii{parameter}{formal} |
| 321 | \indexii{parameter}{actual} |
| 322 | |
| 323 | \begin{itemize} |
| 324 | |
| 325 | \item |
| 326 | If there are no formal parameters, there must be no arguments. |
| 327 | |
| 328 | \item |
| 329 | If the formal parameter list does not end in a star followed by an |
| 330 | identifier, there must be exactly as many arguments as there are |
| 331 | parameters in the formal parameter list (at the top level); the |
| 332 | arguments are assigned to the formal parameters one by one. Note that |
| 333 | the presence or absence of a trailing comma at the top level in either |
| 334 | the formal or the actual parameter list makes no difference. The |
| 335 | assignment to a formal parameter is performed as if the parameter |
| 336 | occurs on the left hand side of an assignment statement whose right |
| 337 | hand side's value is that of the argument. |
| 338 | |
| 339 | \item |
| 340 | If the formal parameter list ends in a star followed by an identifier, |
| 341 | preceded by zero or more comma-followed parameters, there must be at |
| 342 | least as many arguments as there are parameters preceding the star. |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 343 | Call this number \var{N}. The first \var{N} arguments are assigned to |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 344 | the corresponding formal parameters in the way descibed above. A |
| 345 | tuple containing the remaining arguments, if any, is then assigned to |
| 346 | the identifier following the star. This variable will always be a |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 347 | tuple: if there are no extra arguments, its value is \code{()}, if |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 348 | there is just one extra argument, it is a singleton tuple. |
| 349 | \indexii{variable length}{parameter list} |
| 350 | |
| 351 | \end{itemize} |
| 352 | |
| 353 | Note that the `variable length parameter list' feature only works at |
| 354 | the top level of the parameter list; individual parameters use a model |
| 355 | corresponding more closely to that of ordinary assignment. While the |
| 356 | latter model is generally preferable, because of the greater type |
| 357 | safety it offers (wrong-sized tuples aren't silently mistreated), |
| 358 | variable length parameter lists are a sufficiently accepted practice |
| 359 | in most programming languages that a compromise has been worked out. |
| 360 | (And anyway, assignment has no equivalent for empty argument lists.) |
| 361 | |
| 362 | It is also possible to create anonymous functions (functions not bound |
| 363 | to a name), for immediate use in expressions. This uses lambda forms, |
| 364 | described in section \ref{lambda}. |
| 365 | \indexii{lambda}{form} |
| 366 | |
| 367 | \section{Class definitions} \label{class} |
| 368 | \indexii{class}{definition} |
| 369 | |
| 370 | A class definition defines a class object (see section \ref{types}): |
| 371 | \obindex{class} |
| 372 | |
| 373 | \begin{verbatim} |
| 374 | classdef: "class" classname [inheritance] ":" suite |
| 375 | inheritance: "(" [condition_list] ")" |
| 376 | classname: identifier |
| 377 | \end{verbatim} |
| 378 | |
| 379 | A class definition is an executable statement. It first evaluates the |
| 380 | inheritance list, if present. Each item in the inheritance list |
| 381 | should evaluate to a class object. The class's suite is then executed |
| 382 | in a new execution frame (see section \ref{execframes}), using a newly |
| 383 | created local name space and the original global name space. |
| 384 | (Usually, the suite contains only function definitions.) When the |
| 385 | class's suite finishes execution, its execution frame is discarded but |
| 386 | its local name space is saved. A class object is then created using |
| 387 | the inheritance list for the base classes and the saved local name |
| 388 | space for the attribute dictionary. The class name is bound to this |
| 389 | class object in the original local name space. |
| 390 | \index{inheritance} |
| 391 | \indexii{class}{name} |
| 392 | \indexii{name}{binding} |
| 393 | \indexii{execution}{frame} |