blob: c9e33f9b6a637767c6125ae600893af3d96e6e20 [file] [log] [blame]
Fred Drake61c77281998-07-28 19:34:22 +00001\chapter{Compound statements\label{compound}}
Fred Drakef6669171998-05-06 19:52:49 +00002\indexii{compound}{statement}
3
4Compound statements contain (groups of) other statements; they affect
5or control the execution of those other statements in some way. In
6general, compound statements span multiple lines, although in simple
7incarnations a whole compound statement may be contained in one line.
8
Fred Drake5c07d9b1998-05-14 19:37:06 +00009The \keyword{if}, \keyword{while} and \keyword{for} statements implement
10traditional control flow constructs. \keyword{try} specifies exception
Fred Drakef6669171998-05-06 19:52:49 +000011handlers and/or cleanup code for a group of statements. Function and
12class definitions are also syntactically compound statements.
13
Guido van Rossum5399d681998-07-24 18:51:11 +000014Compound statements consist of one or more `clauses.' A clause
15consists of a header and a `suite.' The clause headers of a
Fred Drakef6669171998-05-06 19:52:49 +000016particular compound statement are all at the same indentation level.
17Each clause header begins with a uniquely identifying keyword and ends
18with a colon. A suite is a group of statements controlled by a
19clause. A suite can be one or more semicolon-separated simple
20statements on the same line as the header, following the header's
21colon, or it can be one or more indented statements on subsequent
22lines. Only the latter form of suite can contain nested compound
23statements; the following is illegal, mostly because it wouldn't be
Fred Drake5c07d9b1998-05-14 19:37:06 +000024clear to which \keyword{if} clause a following \keyword{else} clause would
Fred Drakef6669171998-05-06 19:52:49 +000025belong:
26\index{clause}
27\index{suite}
28
29\begin{verbatim}
30if test1: if test2: print x
31\end{verbatim}
32
33Also note that the semicolon binds tighter than the colon in this
34context, so that in the following example, either all or none of the
Fred Drake5c07d9b1998-05-14 19:37:06 +000035\keyword{print} statements are executed:
Fred Drakef6669171998-05-06 19:52:49 +000036
37\begin{verbatim}
38if x < y < z: print x; print y; print z
39\end{verbatim}
40
41Summarizing:
42
Fred Drakecb4638a2001-07-06 22:49:53 +000043\begin{productionlist}
44 \production{compound_stmt}
45 {\token{if_stmt} | \token{while_stmt} | \token{for_stmt}
46 | \token{try_stmt} | \token{funcdef} | \token{classdef}}
47 \production{suite}
48 {\token{stmt_list} NEWLINE
49 | NEWLINE INDENT \token{statement}+ DEDENT}
50 \production{statement}
51 {\token{stmt_list} NEWLINE | \token{compound_stmt}}
52 \production{stmt_list}
53 {\token{simple_stmt} (";" \token{simple_stmt})* [";"]}
54\end{productionlist}
Fred Drakef6669171998-05-06 19:52:49 +000055
Guido van Rossum5399d681998-07-24 18:51:11 +000056Note that statements always end in a
57\code{NEWLINE}\index{NEWLINE token} possibly followed by a
58\code{DEDENT}.\index{DEDENT token} Also note that optional
59continuation clauses always begin with a keyword that cannot start a
60statement, thus there are no ambiguities (the `dangling
61\keyword{else}' problem is solved in Python by requiring nested
62\keyword{if} statements to be indented).
Fred Drakef6669171998-05-06 19:52:49 +000063\indexii{dangling}{else}
64
65The formatting of the grammar rules in the following sections places
66each clause on a separate line for clarity.
67
Fred Drake2829f1c2001-06-23 05:27:20 +000068
Fred Drake61c77281998-07-28 19:34:22 +000069\section{The \keyword{if} statement\label{if}}
Fred Drakef6669171998-05-06 19:52:49 +000070\stindex{if}
71
Fred Drake5c07d9b1998-05-14 19:37:06 +000072The \keyword{if} statement is used for conditional execution:
Fred Drakef6669171998-05-06 19:52:49 +000073
Fred Drakecb4638a2001-07-06 22:49:53 +000074\begin{productionlist}
75 \production{if_stmt}
76 {"if" \token{expression} ":" \token{suite}
77 ( "elif" \token{expression} ":" \token{suite} )*
78 ["else" ":" \token{suite}]}
79\end{productionlist}
Fred Drakef6669171998-05-06 19:52:49 +000080
Guido van Rossum5399d681998-07-24 18:51:11 +000081It selects exactly one of the suites by evaluating the expressions one
Fred Drakef6669171998-05-06 19:52:49 +000082by one until one is found to be true (see section \ref{Booleans} for
83the definition of true and false); then that suite is executed (and no
Fred Drake5c07d9b1998-05-14 19:37:06 +000084other part of the \keyword{if} statement is executed or evaluated). If
Guido van Rossum5399d681998-07-24 18:51:11 +000085all expressions are false, the suite of the \keyword{else} clause, if
Fred Drakef6669171998-05-06 19:52:49 +000086present, is executed.
87\kwindex{elif}
88\kwindex{else}
89
Fred Drake2829f1c2001-06-23 05:27:20 +000090
Fred Drake61c77281998-07-28 19:34:22 +000091\section{The \keyword{while} statement\label{while}}
Fred Drakef6669171998-05-06 19:52:49 +000092\stindex{while}
93\indexii{loop}{statement}
94
Guido van Rossum5399d681998-07-24 18:51:11 +000095The \keyword{while} statement is used for repeated execution as long
96as an expression is true:
Fred Drakef6669171998-05-06 19:52:49 +000097
Fred Drakecb4638a2001-07-06 22:49:53 +000098\begin{productionlist}
99 \production{while_stmt}
100 {"while" \token{expression} ":" \token{suite}
101 ["else" ":" \token{suite}]}
102\end{productionlist}
Fred Drakef6669171998-05-06 19:52:49 +0000103
Guido van Rossum5399d681998-07-24 18:51:11 +0000104This repeatedly tests the expression and, if it is true, executes the
105first suite; if the expression is false (which may be the first time it
Fred Drake5c07d9b1998-05-14 19:37:06 +0000106is tested) the suite of the \keyword{else} clause, if present, is
Fred Drakef6669171998-05-06 19:52:49 +0000107executed and the loop terminates.
108\kwindex{else}
109
Fred Drake5c07d9b1998-05-14 19:37:06 +0000110A \keyword{break} statement executed in the first suite terminates the
111loop without executing the \keyword{else} clause's suite. A
112\keyword{continue} statement executed in the first suite skips the rest
Guido van Rossum5399d681998-07-24 18:51:11 +0000113of the suite and goes back to testing the expression.
Fred Drakef6669171998-05-06 19:52:49 +0000114\stindex{break}
115\stindex{continue}
116
Fred Drake2829f1c2001-06-23 05:27:20 +0000117
Fred Drake61c77281998-07-28 19:34:22 +0000118\section{The \keyword{for} statement\label{for}}
Fred Drakef6669171998-05-06 19:52:49 +0000119\stindex{for}
120\indexii{loop}{statement}
121
Fred Drake5c07d9b1998-05-14 19:37:06 +0000122The \keyword{for} statement is used to iterate over the elements of a
Fred Drake93852ef2001-06-23 06:06:52 +0000123sequence (such as a string, tuple or list) or other iterable object:
Fred Drakef6669171998-05-06 19:52:49 +0000124\obindex{sequence}
125
Fred Drakecb4638a2001-07-06 22:49:53 +0000126\begin{productionlist}
127 \production{for_stmt}
128 {"for" \token{target_list} "in" \token{expression_list}
129 ":" \token{suite}
130 ["else" ":" \token{suite}]}
131\end{productionlist}
Fred Drakef6669171998-05-06 19:52:49 +0000132
Guido van Rossum5399d681998-07-24 18:51:11 +0000133The expression list is evaluated once; it should yield a sequence. The
Fred Drakef6669171998-05-06 19:52:49 +0000134suite is then executed once for each item in the sequence, in the
135order of ascending indices. Each item in turn is assigned to the
136target list using the standard rules for assignments, and then the
137suite is executed. When the items are exhausted (which is immediately
Fred Drake5c07d9b1998-05-14 19:37:06 +0000138when the sequence is empty), the suite in the \keyword{else} clause, if
Fred Drakef6669171998-05-06 19:52:49 +0000139present, is executed, and the loop terminates.
140\kwindex{in}
141\kwindex{else}
142\indexii{target}{list}
143
Fred Drake5c07d9b1998-05-14 19:37:06 +0000144A \keyword{break} statement executed in the first suite terminates the
145loop without executing the \keyword{else} clause's suite. A
146\keyword{continue} statement executed in the first suite skips the rest
147of the suite and continues with the next item, or with the \keyword{else}
Fred Drakef6669171998-05-06 19:52:49 +0000148clause if there was no next item.
149\stindex{break}
150\stindex{continue}
151
152The suite may assign to the variable(s) in the target list; this does
153not affect the next item assigned to it.
154
155The target list is not deleted when the loop is finished, but if the
156sequence is empty, it will not have been assigned to at all by the
Guido van Rossum5399d681998-07-24 18:51:11 +0000157loop. Hint: the built-in function \function{range()} returns a
158sequence of integers suitable to emulate the effect of Pascal's
Fred Drake5c07d9b1998-05-14 19:37:06 +0000159\code{for i := a to b do};
Guido van Rossum5399d681998-07-24 18:51:11 +0000160e.g., \code{range(3)} returns the list \code{[0, 1, 2]}.
Fred Drakef6669171998-05-06 19:52:49 +0000161\bifuncindex{range}
Fred Drake5c07d9b1998-05-14 19:37:06 +0000162\indexii{Pascal}{language}
Fred Drakef6669171998-05-06 19:52:49 +0000163
Fred Drake5c07d9b1998-05-14 19:37:06 +0000164\strong{Warning:} There is a subtlety when the sequence is being modified
Fred Drakef6669171998-05-06 19:52:49 +0000165by the loop (this can only occur for mutable sequences, i.e. lists).
166An internal counter is used to keep track of which item is used next,
167and this is incremented on each iteration. When this counter has
168reached the length of the sequence the loop terminates. This means that
169if the suite deletes the current (or a previous) item from the
170sequence, the next item will be skipped (since it gets the index of
171the current item which has already been treated). Likewise, if the
172suite inserts an item in the sequence before the current item, the
173current item will be treated again the next time through the loop.
174This can lead to nasty bugs that can be avoided by making a temporary
Guido van Rossum5399d681998-07-24 18:51:11 +0000175copy using a slice of the whole sequence, e.g.,
Fred Drakef6669171998-05-06 19:52:49 +0000176\index{loop!over mutable sequence}
177\index{mutable sequence!loop over}
178
179\begin{verbatim}
180for x in a[:]:
181 if x < 0: a.remove(x)
182\end{verbatim}
183
Fred Drake2829f1c2001-06-23 05:27:20 +0000184
Fred Drake61c77281998-07-28 19:34:22 +0000185\section{The \keyword{try} statement\label{try}}
Fred Drakef6669171998-05-06 19:52:49 +0000186\stindex{try}
187
Fred Drake5c07d9b1998-05-14 19:37:06 +0000188The \keyword{try} statement specifies exception handlers and/or cleanup
Fred Drakef6669171998-05-06 19:52:49 +0000189code for a group of statements:
190
Fred Drakecb4638a2001-07-06 22:49:53 +0000191\begin{productionlist}
192 \production{try_stmt}
193 {\token{try_exc_stmt} | \token{try_fin_stmt}}
194 \production{try_exc_stmt}
195 {"try" ":" \token{suite}
196 ("except" [\token{expression} ["," \token{target}]] ":"
197 \token{suite})+
198 ["else" ":" \token{suite}]}
199 \production{try_fin_stmt}
200 {"try" ":" \token{suite}
201 "finally" ":" \token{suite}}
202\end{productionlist}
Fred Drakef6669171998-05-06 19:52:49 +0000203
Fred Drake5c07d9b1998-05-14 19:37:06 +0000204There are two forms of \keyword{try} statement:
205\keyword{try}...\keyword{except} and
Guido van Rossum5399d681998-07-24 18:51:11 +0000206\keyword{try}...\keyword{finally}. These forms cannot be mixed (but
207they can be nested in each other).
Fred Drakef6669171998-05-06 19:52:49 +0000208
Fred Drake5c07d9b1998-05-14 19:37:06 +0000209The \keyword{try}...\keyword{except} form specifies one or more
210exception handlers
211(the \keyword{except} clauses). When no exception occurs in the
212\keyword{try} clause, no exception handler is executed. When an
213exception occurs in the \keyword{try} suite, a search for an exception
Guido van Rossum5399d681998-07-24 18:51:11 +0000214handler is started. This search inspects the except clauses in turn until
215one is found that matches the exception. An expression-less except
Fred Drakef6669171998-05-06 19:52:49 +0000216clause, if present, must be last; it matches any exception. For an
Guido van Rossum5399d681998-07-24 18:51:11 +0000217except clause with an expression, that expression is evaluated, and the
Fred Drakef6669171998-05-06 19:52:49 +0000218clause matches the exception if the resulting object is ``compatible''
219with the exception. An object is compatible with an exception if it
220is either the object that identifies the exception, or (for exceptions
221that are classes) it is a base class of the exception, or it is a
222tuple containing an item that is compatible with the exception. Note
223that the object identities must match, i.e. it must be the same
224object, not just an object with the same value.
225\kwindex{except}
226
227If no except clause matches the exception, the search for an exception
228handler continues in the surrounding code and on the invocation stack.
229
Guido van Rossum5399d681998-07-24 18:51:11 +0000230If the evaluation of an expression in the header of an except clause
Thomas Woutersf9b526d2000-07-16 19:05:38 +0000231raises an exception, the original search for a handler is canceled
Fred Drakef6669171998-05-06 19:52:49 +0000232and a search starts for the new exception in the surrounding code and
Fred Drake5c07d9b1998-05-14 19:37:06 +0000233on the call stack (it is treated as if the entire \keyword{try} statement
Fred Drakef6669171998-05-06 19:52:49 +0000234raised the exception).
235
236When a matching except clause is found, the exception's parameter is
237assigned to the target specified in that except clause, if present,
Fred Drake4c2533f1999-08-24 22:14:01 +0000238and the except clause's suite is executed. All except clauses must
239have an executable block. When the end of this block
Fred Drakef6669171998-05-06 19:52:49 +0000240is reached, execution continues normally after the entire try
241statement. (This means that if two nested handlers exist for the same
242exception, and the exception occurs in the try clause of the inner
243handler, the outer handler will not handle the exception.)
244
245Before an except clause's suite is executed, details about the
Fred Drake99cd5731999-02-12 20:40:09 +0000246exception are assigned to three variables in the
247\module{sys}\refbimodindex{sys} module: \code{sys.exc_type} receives
248the object identifying the exception; \code{sys.exc_value} receives
249the exception's parameter; \code{sys.exc_traceback} receives a
250traceback object\obindex{traceback} (see section \ref{traceback})
251identifying the point in the program where the exception occurred.
Guido van Rossum5399d681998-07-24 18:51:11 +0000252These details are also available through the \function{sys.exc_info()}
Fred Drake99cd5731999-02-12 20:40:09 +0000253function, which returns a tuple \code{(\var{exc_type}, \var{exc_value},
254\var{exc_traceback})}. Use of the corresponding variables is
Guido van Rossum5399d681998-07-24 18:51:11 +0000255deprecated in favor of this function, since their use is unsafe in a
256threaded program. As of Python 1.5, the variables are restored to
257their previous values (before the call) when returning from a function
258that handled an exception.
Fred Drake99cd5731999-02-12 20:40:09 +0000259\withsubitem{(in module sys)}{\ttindex{exc_type}
260 \ttindex{exc_value}\ttindex{exc_traceback}}
Fred Drakef6669171998-05-06 19:52:49 +0000261
Fred Drake2cba0f62001-01-02 19:22:48 +0000262The optional \keyword{else} clause is executed if and when control
263flows off the end of the \keyword{try} clause.\footnote{
264 Currently, control ``flows off the end'' except in the case of an
265 exception or the execution of a \keyword{return},
266 \keyword{continue}, or \keyword{break} statement.
267} Exceptions in the \keyword{else} clause are not handled by the
268preceding \keyword{except} clauses.
Fred Drakef6669171998-05-06 19:52:49 +0000269\kwindex{else}
Fred Drake2cba0f62001-01-02 19:22:48 +0000270\stindex{return}
271\stindex{break}
272\stindex{continue}
Fred Drakef6669171998-05-06 19:52:49 +0000273
Fred Drake5c07d9b1998-05-14 19:37:06 +0000274The \keyword{try}...\keyword{finally} form specifies a `cleanup' handler. The
275\keyword{try} clause is executed. When no exception occurs, the
276\keyword{finally} clause is executed. When an exception occurs in the
277\keyword{try} clause, the exception is temporarily saved, the
278\keyword{finally} clause is executed, and then the saved exception is
279re-raised. If the \keyword{finally} clause raises another exception or
Jeremy Hylton3faa52e2001-02-01 22:48:12 +0000280executes a \keyword{return} or \keyword{break} statement, the saved
281exception is lost. A \keyword{continue} statement is illegal in the
282\keyword{finally} clause. (The reason is a problem with the current
283implementation -- thsi restriction may be lifted in the future). The
284exception information is not available to the program during execution of
285the \keyword{finally} clause.
Fred Drakef6669171998-05-06 19:52:49 +0000286\kwindex{finally}
287
Jeremy Hylton3faa52e2001-02-01 22:48:12 +0000288When a \keyword{return}, \keyword{break} or \keyword{continue} statement is
289executed in the \keyword{try} suite of a \keyword{try}...\keyword{finally}
290statement, the \keyword{finally} clause is also executed `on the way out.' A
291\keyword{continue} statement is illegal in the \keyword{finally} clause.
292(The reason is a problem with the current implementation --- this
Fred Drakef6669171998-05-06 19:52:49 +0000293restriction may be lifted in the future).
294\stindex{return}
295\stindex{break}
296\stindex{continue}
297
Fred Drake2829f1c2001-06-23 05:27:20 +0000298
Fred Drake61c77281998-07-28 19:34:22 +0000299\section{Function definitions\label{function}}
Fred Drakef6669171998-05-06 19:52:49 +0000300\indexii{function}{definition}
301
302A function definition defines a user-defined function object (see
Guido van Rossum5399d681998-07-24 18:51:11 +0000303section \ref{types}):
Fred Drakef6669171998-05-06 19:52:49 +0000304\obindex{user-defined function}
305\obindex{function}
306
Fred Drakecb4638a2001-07-06 22:49:53 +0000307\begin{productionlist}
308 \production{funcdef}
309 {"def" \token{funcname} "(" [\token{parameter_list}] ")"
310 ":" \token{suite}}
311 \production{parameter_list}
312 {(\token{defparameter} ",")*
313 ("*" \token{identifier} [, "**" \token{identifier}]
314 | "**" \token{identifier}
315 | \token{defparameter} [","])}
316 \production{defparameter}
317 {\token{parameter} ["=" \token{expression}]}
318 \production{sublist}
319 {\token{parameter} ("," \token{parameter})* [","]}
320 \production{parameter}
321 {\token{identifier} | "(" \token{sublist} ")"}
322 \production{funcname}
323 {\token{identifier}}
324\end{productionlist}
Fred Drakef6669171998-05-06 19:52:49 +0000325
326A function definition is an executable statement. Its execution binds
Guido van Rossum5399d681998-07-24 18:51:11 +0000327the function name in the current local namespace to a function object
Fred Drakef6669171998-05-06 19:52:49 +0000328(a wrapper around the executable code for the function). This
Guido van Rossum5399d681998-07-24 18:51:11 +0000329function object contains a reference to the current global namespace
330as the global namespace to be used when the function is called.
Fred Drakef6669171998-05-06 19:52:49 +0000331\indexii{function}{name}
332\indexii{name}{binding}
333
334The function definition does not execute the function body; this gets
335executed only when the function is called.
336
Guido van Rossum5399d681998-07-24 18:51:11 +0000337When one or more top-level parameters have the form \var{parameter}
338\code{=} \var{expression}, the function is said to have ``default
Guido van Rossume0394391998-12-04 19:37:10 +0000339parameter values.'' For a parameter with a
Guido van Rossum5399d681998-07-24 18:51:11 +0000340default value, the corresponding argument may be omitted from a call,
341in which case the parameter's default value is substituted. If a
342parameter has a default value, all following parameters must also have
343a default value --- this is a syntactic restriction that is not
Fred Drakee15956b2000-04-03 04:51:13 +0000344expressed by the grammar.
Fred Drakef6669171998-05-06 19:52:49 +0000345\indexiii{default}{parameter}{value}
346
Guido van Rossume0394391998-12-04 19:37:10 +0000347\strong{Default parameter values are evaluated when the function
348definition is executed.} This means that the expression is evaluated
349once, when the function is defined, and that that same
350``pre-computed'' value is used for each call. This is especially
351important to understand when a default parameter is a mutable object,
352such as a list or a dictionary: if the function modifies the object
353(e.g. by appending an item to a list), the default value is in effect
354modified. This is generally not what was intended. A way around this
355is to use \code{None} as the default, and explicitly test for it in
356the body of the function, e.g.:
357
358\begin{verbatim}
359def whats_on_the_telly(penguin=None):
360 if penguin is None:
361 penguin = []
362 penguin.append("property of the zoo")
363 return penguin
364\end{verbatim}
365
Guido van Rossum5399d681998-07-24 18:51:11 +0000366Function call semantics are described in more detail in section
367\ref{calls}.
368A function call always assigns values to all parameters mentioned in
369the parameter list, either from position arguments, from keyword
370arguments, or from default values. If the form ``\code{*identifier}''
371is present, it is initialized to a tuple receiving any excess
372positional parameters, defaulting to the empty tuple. If the form
373``\code{**identifier}'' is present, it is initialized to a new
374dictionary receiving any excess keyword arguments, defaulting to a
375new empty dictionary.
Fred Drakef6669171998-05-06 19:52:49 +0000376
Fred Drakef6669171998-05-06 19:52:49 +0000377It is also possible to create anonymous functions (functions not bound
378to a name), for immediate use in expressions. This uses lambda forms,
Guido van Rossum5399d681998-07-24 18:51:11 +0000379described in section \ref{lambda}. Note that the lambda form is
380merely a shorthand for a simplified function definition; a function
381defined in a ``\keyword{def}'' statement can be passed around or
382assigned to another name just like a function defined by a lambda
383form. The ``\keyword{def}'' form is actually more powerful since it
384allows the execution of multiple statements.
Fred Drakef6669171998-05-06 19:52:49 +0000385\indexii{lambda}{form}
386
Guido van Rossum5399d681998-07-24 18:51:11 +0000387\strong{Programmer's note:} a ``\code{def}'' form executed inside a
388function definition defines a local function that can be returned or
Jeremy Hyltonaa90adc2001-03-23 17:23:50 +0000389passed around. The semantics of name resolution in the nested
390function will change in Python 2.2. See the appendix for a
391description of the new semantics.
Guido van Rossum5399d681998-07-24 18:51:11 +0000392
Fred Drake2829f1c2001-06-23 05:27:20 +0000393
Fred Drake61c77281998-07-28 19:34:22 +0000394\section{Class definitions\label{class}}
Fred Drakef6669171998-05-06 19:52:49 +0000395\indexii{class}{definition}
396
397A class definition defines a class object (see section \ref{types}):
398\obindex{class}
399
Fred Drakecb4638a2001-07-06 22:49:53 +0000400\begin{productionlist}
401 \production{classdef}
402 {"class" \token{classname} [\token{inheritance}] ":"
403 \token{suite}}
404 \production{inheritance}
405 {"(" [\token{expression_list}] ")"}
406 \production{classname}
407 {\token{identifier}}
408\end{productionlist}
Fred Drakef6669171998-05-06 19:52:49 +0000409
410A class definition is an executable statement. It first evaluates the
411inheritance list, if present. Each item in the inheritance list
412should evaluate to a class object. The class's suite is then executed
413in a new execution frame (see section \ref{execframes}), using a newly
Guido van Rossum5399d681998-07-24 18:51:11 +0000414created local namespace and the original global namespace.
Fred Drakef6669171998-05-06 19:52:49 +0000415(Usually, the suite contains only function definitions.) When the
416class's suite finishes execution, its execution frame is discarded but
Guido van Rossum5399d681998-07-24 18:51:11 +0000417its local namespace is saved. A class object is then created using
418the inheritance list for the base classes and the saved local
419namespace for the attribute dictionary. The class name is bound to this
420class object in the original local namespace.
Fred Drakef6669171998-05-06 19:52:49 +0000421\index{inheritance}
422\indexii{class}{name}
423\indexii{name}{binding}
424\indexii{execution}{frame}
Guido van Rossum5399d681998-07-24 18:51:11 +0000425
426\strong{Programmer's note:} variables defined in the class definition
427are class variables; they are shared by all instances. To define
428instance variables, they must be given a value in the the
429\method{__init__()} method or in another method. Both class and
430instance variables are accessible through the notation
Fred Drake7c116d72001-05-10 15:09:36 +0000431``\code{self.name}'', and an instance variable hides a class variable
Guido van Rossum5399d681998-07-24 18:51:11 +0000432with the same name when accessed in this way. Class variables with
433immutable values can be used as defaults for instance variables.