blob: a661503f87f7337ffe8d739ac6393ba299d9f4a2 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2.. _compound:
3
4*******************
5Compound statements
6*******************
7
8.. index:: pair: compound; statement
9
10Compound statements contain (groups of) other statements; they affect or control
11the execution of those other statements in some way. In general, compound
12statements span multiple lines, although in simple incarnations a whole compound
13statement may be contained in one line.
14
15The :keyword:`if`, :keyword:`while` and :keyword:`for` statements implement
16traditional control flow constructs. :keyword:`try` specifies exception
Georg Brandl02c30562007-09-07 17:52:53 +000017handlers and/or cleanup code for a group of statements, while the
18:keyword:`with` statement allows the execution of initialization and
19finalization code around a block of code. Function and class definitions are
20also syntactically compound statements.
Georg Brandl116aa622007-08-15 14:28:22 +000021
22.. index::
23 single: clause
24 single: suite
25
26Compound statements consist of one or more 'clauses.' A clause consists of a
27header and a 'suite.' The clause headers of a particular compound statement are
28all at the same indentation level. Each clause header begins with a uniquely
29identifying keyword and ends with a colon. A suite is a group of statements
30controlled by a clause. A suite can be one or more semicolon-separated simple
31statements on the same line as the header, following the header's colon, or it
32can be one or more indented statements on subsequent lines. Only the latter
33form of suite can contain nested compound statements; the following is illegal,
34mostly because it wouldn't be clear to which :keyword:`if` clause a following
Georg Brandl02c30562007-09-07 17:52:53 +000035:keyword:`else` clause would belong::
Georg Brandl116aa622007-08-15 14:28:22 +000036
Georg Brandl6911e3c2007-09-04 07:15:32 +000037 if test1: if test2: print(x)
Georg Brandl116aa622007-08-15 14:28:22 +000038
39Also note that the semicolon binds tighter than the colon in this context, so
Georg Brandl6911e3c2007-09-04 07:15:32 +000040that in the following example, either all or none of the :func:`print` calls are
41executed::
Georg Brandl116aa622007-08-15 14:28:22 +000042
Georg Brandl6911e3c2007-09-04 07:15:32 +000043 if x < y < z: print(x); print(y); print(z)
Georg Brandl116aa622007-08-15 14:28:22 +000044
45Summarizing:
46
47.. productionlist::
48 compound_stmt: `if_stmt`
49 : | `while_stmt`
50 : | `for_stmt`
51 : | `try_stmt`
52 : | `with_stmt`
53 : | `funcdef`
54 : | `classdef`
55 suite: `stmt_list` NEWLINE | NEWLINE INDENT `statement`+ DEDENT
56 statement: `stmt_list` NEWLINE | `compound_stmt`
57 stmt_list: `simple_stmt` (";" `simple_stmt`)* [";"]
58
59.. index::
60 single: NEWLINE token
61 single: DEDENT token
62 pair: dangling; else
63
64Note that statements always end in a ``NEWLINE`` possibly followed by a
Georg Brandl02c30562007-09-07 17:52:53 +000065``DEDENT``. Also note that optional continuation clauses always begin with a
Georg Brandl116aa622007-08-15 14:28:22 +000066keyword that cannot start a statement, thus there are no ambiguities (the
67'dangling :keyword:`else`' problem is solved in Python by requiring nested
68:keyword:`if` statements to be indented).
69
70The formatting of the grammar rules in the following sections places each clause
71on a separate line for clarity.
72
73
74.. _if:
75
76The :keyword:`if` statement
77===========================
78
79.. index:: statement: if
Georg Brandl02c30562007-09-07 17:52:53 +000080 keyword: elif
81 keyword: else
Georg Brandl116aa622007-08-15 14:28:22 +000082
83The :keyword:`if` statement is used for conditional execution:
84
85.. productionlist::
86 if_stmt: "if" `expression` ":" `suite`
87 : ( "elif" `expression` ":" `suite` )*
88 : ["else" ":" `suite`]
89
Georg Brandl116aa622007-08-15 14:28:22 +000090It selects exactly one of the suites by evaluating the expressions one by one
91until one is found to be true (see section :ref:`booleans` for the definition of
92true and false); then that suite is executed (and no other part of the
93:keyword:`if` statement is executed or evaluated). If all expressions are
94false, the suite of the :keyword:`else` clause, if present, is executed.
95
96
97.. _while:
98
99The :keyword:`while` statement
100==============================
101
102.. index::
103 statement: while
Georg Brandl02c30562007-09-07 17:52:53 +0000104 keyword: else
Georg Brandl116aa622007-08-15 14:28:22 +0000105 pair: loop; statement
106
107The :keyword:`while` statement is used for repeated execution as long as an
108expression is true:
109
110.. productionlist::
111 while_stmt: "while" `expression` ":" `suite`
112 : ["else" ":" `suite`]
113
Georg Brandl116aa622007-08-15 14:28:22 +0000114This repeatedly tests the expression and, if it is true, executes the first
115suite; if the expression is false (which may be the first time it is tested) the
116suite of the :keyword:`else` clause, if present, is executed and the loop
117terminates.
118
119.. index::
120 statement: break
121 statement: continue
122
123A :keyword:`break` statement executed in the first suite terminates the loop
124without executing the :keyword:`else` clause's suite. A :keyword:`continue`
125statement executed in the first suite skips the rest of the suite and goes back
126to testing the expression.
127
128
129.. _for:
130
131The :keyword:`for` statement
132============================
133
134.. index::
135 statement: for
Georg Brandl02c30562007-09-07 17:52:53 +0000136 keyword: in
137 keyword: else
138 pair: target; list
Georg Brandl116aa622007-08-15 14:28:22 +0000139 pair: loop; statement
Georg Brandl02c30562007-09-07 17:52:53 +0000140 object: sequence
Georg Brandl116aa622007-08-15 14:28:22 +0000141
142The :keyword:`for` statement is used to iterate over the elements of a sequence
143(such as a string, tuple or list) or other iterable object:
144
145.. productionlist::
146 for_stmt: "for" `target_list` "in" `expression_list` ":" `suite`
147 : ["else" ":" `suite`]
148
Georg Brandl116aa622007-08-15 14:28:22 +0000149The expression list is evaluated once; it should yield an iterable object. An
150iterator is created for the result of the ``expression_list``. The suite is
151then executed once for each item provided by the iterator, in the order of
152ascending indices. Each item in turn is assigned to the target list using the
Georg Brandl02c30562007-09-07 17:52:53 +0000153standard rules for assignments (see :ref:`assignment`), and then the suite is
154executed. When the items are exhausted (which is immediately when the sequence
155is empty or an iterator raises a :exc:`StopIteration` exception), the suite in
Georg Brandl116aa622007-08-15 14:28:22 +0000156the :keyword:`else` clause, if present, is executed, and the loop terminates.
157
158.. index::
159 statement: break
160 statement: continue
161
162A :keyword:`break` statement executed in the first suite terminates the loop
163without executing the :keyword:`else` clause's suite. A :keyword:`continue`
164statement executed in the first suite skips the rest of the suite and continues
165with the next item, or with the :keyword:`else` clause if there was no next
166item.
167
168The suite may assign to the variable(s) in the target list; this does not affect
169the next item assigned to it.
170
171.. index::
172 builtin: range
Georg Brandl116aa622007-08-15 14:28:22 +0000173
Georg Brandl02c30562007-09-07 17:52:53 +0000174Names in the target list are not deleted when the loop is finished, but if the
175sequence is empty, it will not have been assigned to at all by the loop. Hint:
176the built-in function :func:`range` returns an iterator of integers suitable to
177emulate the effect of Pascal's ``for i := a to b do``; e.g., ``range(3)``
178returns the list ``[0, 1, 2]``.
Georg Brandl116aa622007-08-15 14:28:22 +0000179
180.. warning::
181
182 .. index::
183 single: loop; over mutable sequence
184 single: mutable sequence; loop over
185
186 There is a subtlety when the sequence is being modified by the loop (this can
Georg Brandl02c30562007-09-07 17:52:53 +0000187 only occur for mutable sequences, i.e. lists). An internal counter is used
188 to keep track of which item is used next, and this is incremented on each
Georg Brandl116aa622007-08-15 14:28:22 +0000189 iteration. When this counter has reached the length of the sequence the loop
190 terminates. This means that if the suite deletes the current (or a previous)
Georg Brandl02c30562007-09-07 17:52:53 +0000191 item from the sequence, the next item will be skipped (since it gets the
192 index of the current item which has already been treated). Likewise, if the
193 suite inserts an item in the sequence before the current item, the current
194 item will be treated again the next time through the loop. This can lead to
195 nasty bugs that can be avoided by making a temporary copy using a slice of
196 the whole sequence, e.g., ::
Georg Brandl116aa622007-08-15 14:28:22 +0000197
Georg Brandl02c30562007-09-07 17:52:53 +0000198 for x in a[:]:
199 if x < 0: a.remove(x)
Georg Brandl116aa622007-08-15 14:28:22 +0000200
201
202.. _try:
203
204The :keyword:`try` statement
205============================
206
207.. index:: statement: try
Georg Brandl16174572007-09-01 12:38:06 +0000208.. index:: keyword: except
Georg Brandl116aa622007-08-15 14:28:22 +0000209
210The :keyword:`try` statement specifies exception handlers and/or cleanup code
211for a group of statements:
212
213.. productionlist::
214 try_stmt: try1_stmt | try2_stmt
215 try1_stmt: "try" ":" `suite`
Georg Brandl0068e2c2007-09-06 14:03:41 +0000216 : ("except" [`expression` ["as" `target`]] ":" `suite`)+
Georg Brandl116aa622007-08-15 14:28:22 +0000217 : ["else" ":" `suite`]
218 : ["finally" ":" `suite`]
219 try2_stmt: "try" ":" `suite`
220 : "finally" ":" `suite`
221
Georg Brandl0068e2c2007-09-06 14:03:41 +0000222The :keyword:`except` clause(s) specify one or more exception handlers. When no
Georg Brandl116aa622007-08-15 14:28:22 +0000223exception occurs in the :keyword:`try` clause, no exception handler is executed.
224When an exception occurs in the :keyword:`try` suite, a search for an exception
225handler is started. This search inspects the except clauses in turn until one
226is found that matches the exception. An expression-less except clause, if
227present, must be last; it matches any exception. For an except clause with an
228expression, that expression is evaluated, and the clause matches the exception
229if the resulting object is "compatible" with the exception. An object is
230compatible with an exception if it is the class or a base class of the exception
231object or a tuple containing an item compatible with the exception.
232
233If no except clause matches the exception, the search for an exception handler
234continues in the surrounding code and on the invocation stack. [#]_
235
236If the evaluation of an expression in the header of an except clause raises an
237exception, the original search for a handler is canceled and a search starts for
238the new exception in the surrounding code and on the call stack (it is treated
239as if the entire :keyword:`try` statement raised the exception).
240
241When a matching except clause is found, the exception is assigned to the target
Georg Brandl02c30562007-09-07 17:52:53 +0000242specified after the :keyword:`as` keyword in that except clause, if present, and
243the except clause's suite is executed. All except clauses must have an
244executable block. When the end of this block is reached, execution continues
245normally after the entire try statement. (This means that if two nested
246handlers exist for the same exception, and the exception occurs in the try
247clause of the inner handler, the outer handler will not handle the exception.)
248
249When an exception has been assigned using ``as target``, it is cleared at the
250end of the except clause. This is as if ::
251
252 except E as N:
253 foo
254
255was translated to ::
256
257 except E as N:
258 try:
259 foo
260 finally:
261 N = None
262 del N
263
264That means that you have to assign the exception to a different name if you want
265to be able to refer to it after the except clause. The reason for this is that
266with the traceback attached to them, exceptions will form a reference cycle with
267the stack frame, keeping all locals in that frame alive until the next garbage
268collection occurs.
Georg Brandl116aa622007-08-15 14:28:22 +0000269
270.. index::
271 module: sys
272 object: traceback
273
274Before an except clause's suite is executed, details about the exception are
275stored in the :mod:`sys` module and can be access via :func:`sys.exc_info`.
Georg Brandl02c30562007-09-07 17:52:53 +0000276:func:`sys.exc_info` returns a 3-tuple consisting of: ``exc_type``, the
277exception class; ``exc_value``, the exception instance; ``exc_traceback``, a
278traceback object (see section :ref:`types`) identifying the point in the program
279where the exception occurred. :func:`sys.exc_info` values are restored to their
280previous values (before the call) when returning from a function that handled an
281exception.
Georg Brandl116aa622007-08-15 14:28:22 +0000282
283.. index::
284 keyword: else
285 statement: return
286 statement: break
287 statement: continue
288
289The optional :keyword:`else` clause is executed if and when control flows off
290the end of the :keyword:`try` clause. [#]_ Exceptions in the :keyword:`else`
291clause are not handled by the preceding :keyword:`except` clauses.
292
293.. index:: keyword: finally
294
295If :keyword:`finally` is present, it specifies a 'cleanup' handler. The
296:keyword:`try` clause is executed, including any :keyword:`except` and
297:keyword:`else` clauses. If an exception occurs in any of the clauses and is
298not handled, the exception is temporarily saved. The :keyword:`finally` clause
299is executed. If there is a saved exception, it is re-raised at the end of the
300:keyword:`finally` clause. If the :keyword:`finally` clause raises another
301exception or executes a :keyword:`return` or :keyword:`break` statement, the
302saved exception is lost. The exception information is not available to the
303program during execution of the :keyword:`finally` clause.
304
305.. index::
306 statement: return
307 statement: break
308 statement: continue
309
310When a :keyword:`return`, :keyword:`break` or :keyword:`continue` statement is
311executed in the :keyword:`try` suite of a :keyword:`try`...\ :keyword:`finally`
312statement, the :keyword:`finally` clause is also executed 'on the way out.' A
313:keyword:`continue` statement is illegal in the :keyword:`finally` clause. (The
314reason is a problem with the current implementation --- this restriction may be
315lifted in the future).
316
317Additional information on exceptions can be found in section :ref:`exceptions`,
318and information on using the :keyword:`raise` statement to generate exceptions
319may be found in section :ref:`raise`.
320
Georg Brandl02c30562007-09-07 17:52:53 +0000321.. seealso::
322
323 :pep:`3110` - Catching exceptions in Python 3000
324 Describes the differences in :keyword:`try` statements between Python 2.x
325 and 3.0.
326
Georg Brandl116aa622007-08-15 14:28:22 +0000327
328.. _with:
329
330The :keyword:`with` statement
331=============================
332
333.. index:: statement: with
334
Georg Brandl116aa622007-08-15 14:28:22 +0000335The :keyword:`with` statement is used to wrap the execution of a block with
Georg Brandl02c30562007-09-07 17:52:53 +0000336methods defined by a context manager (see section :ref:`context-managers`).
337This allows common :keyword:`try`...\ :keyword:`except`...\ :keyword:`finally`
338usage patterns to be encapsulated for convenient reuse.
Georg Brandl116aa622007-08-15 14:28:22 +0000339
340.. productionlist::
341 with_stmt: "with" `expression` ["as" `target`] ":" `suite`
342
343The execution of the :keyword:`with` statement proceeds as follows:
344
345#. The context expression is evaluated to obtain a context manager.
346
347#. The context manager's :meth:`__enter__` method is invoked.
348
349#. If a target was included in the :keyword:`with` statement, the return value
350 from :meth:`__enter__` is assigned to it.
351
352 .. note::
353
Georg Brandl02c30562007-09-07 17:52:53 +0000354 The :keyword:`with` statement guarantees that if the :meth:`__enter__`
355 method returns without an error, then :meth:`__exit__` will always be
356 called. Thus, if an error occurs during the assignment to the target
357 list, it will be treated the same as an error occurring within the suite
358 would be. See step 5 below.
Georg Brandl116aa622007-08-15 14:28:22 +0000359
360#. The suite is executed.
361
Georg Brandl02c30562007-09-07 17:52:53 +0000362#. The context manager's :meth:`__exit__` method is invoked. If an exception
Georg Brandl116aa622007-08-15 14:28:22 +0000363 caused the suite to be exited, its type, value, and traceback are passed as
364 arguments to :meth:`__exit__`. Otherwise, three :const:`None` arguments are
365 supplied.
366
367 If the suite was exited due to an exception, and the return value from the
Georg Brandl02c30562007-09-07 17:52:53 +0000368 :meth:`__exit__` method was false, the exception is reraised. If the return
Georg Brandl116aa622007-08-15 14:28:22 +0000369 value was true, the exception is suppressed, and execution continues with the
370 statement following the :keyword:`with` statement.
371
Georg Brandl02c30562007-09-07 17:52:53 +0000372 If the suite was exited for any reason other than an exception, the return
373 value from :meth:`__exit__` is ignored, and execution proceeds at the normal
374 location for the kind of exit that was taken.
Georg Brandl116aa622007-08-15 14:28:22 +0000375
376
377.. seealso::
378
379 :pep:`0343` - The "with" statement
380 The specification, background, and examples for the Python :keyword:`with`
381 statement.
382
383
384.. _function:
385
386Function definitions
387====================
388
389.. index::
390 pair: function; definition
391 statement: def
Georg Brandl116aa622007-08-15 14:28:22 +0000392 object: user-defined function
393 object: function
Georg Brandl02c30562007-09-07 17:52:53 +0000394 pair: function; name
395 pair: name; binding
Georg Brandl116aa622007-08-15 14:28:22 +0000396
397A function definition defines a user-defined function object (see section
398:ref:`types`):
399
400.. productionlist::
401 funcdef: [`decorators`] "def" `funcname` "(" [`parameter_list`] ")" ["->" `expression`]? ":" `suite`
402 decorators: `decorator`+
403 decorator: "@" `dotted_name` ["(" [`argument_list` [","]] ")"] NEWLINE
404 dotted_name: `identifier` ("." `identifier`)*
405 parameter_list: (`defparameter` ",")*
406 : ( "*" [`parameter`] ("," `defparameter`)*
407 : [, "**" `parameter`]
408 : | "**" `parameter`
409 : | `defparameter` [","] )
410 parameter: `identifier` [":" `expression`]
411 defparameter: `parameter` ["=" `expression`]
412 funcname: `identifier`
413
Georg Brandl116aa622007-08-15 14:28:22 +0000414
415A function definition is an executable statement. Its execution binds the
416function name in the current local namespace to a function object (a wrapper
417around the executable code for the function). This function object contains a
418reference to the current global namespace as the global namespace to be used
419when the function is called.
420
421The function definition does not execute the function body; this gets executed
422only when the function is called.
423
424A function definition may be wrapped by one or more decorator expressions.
425Decorator expressions are evaluated when the function is defined, in the scope
426that contains the function definition. The result must be a callable, which is
427invoked with the function object as the only argument. The returned value is
428bound to the function name instead of the function object. Multiple decorators
Georg Brandl02c30562007-09-07 17:52:53 +0000429are applied in nested fashion. For example, the following code ::
Georg Brandl116aa622007-08-15 14:28:22 +0000430
431 @f1(arg)
432 @f2
433 def func(): pass
434
Georg Brandl02c30562007-09-07 17:52:53 +0000435is equivalent to ::
Georg Brandl116aa622007-08-15 14:28:22 +0000436
437 def func(): pass
438 func = f1(arg)(f2(func))
439
440.. index:: triple: default; parameter; value
441
442When one or more parameters have the form *parameter* ``=`` *expression*, the
443function is said to have "default parameter values." For a parameter with a
444default value, the corresponding argument may be omitted from a call, in which
445case the parameter's default value is substituted. If a parameter has a default
Georg Brandl02c30562007-09-07 17:52:53 +0000446value, all following parameters up until the "``*``" must also have a default
447value --- this is a syntactic restriction that is not expressed by the grammar.
Georg Brandl116aa622007-08-15 14:28:22 +0000448
449**Default parameter values are evaluated when the function definition is
Georg Brandl02c30562007-09-07 17:52:53 +0000450executed.** This means that the expression is evaluated once, when the function
Georg Brandl116aa622007-08-15 14:28:22 +0000451is defined, and that that same "pre-computed" value is used for each call. This
452is especially important to understand when a default parameter is a mutable
453object, such as a list or a dictionary: if the function modifies the object
454(e.g. by appending an item to a list), the default value is in effect modified.
Georg Brandl02c30562007-09-07 17:52:53 +0000455This is generally not what was intended. A way around this is to use ``None``
Georg Brandl116aa622007-08-15 14:28:22 +0000456as the default, and explicitly test for it in the body of the function, e.g.::
457
458 def whats_on_the_telly(penguin=None):
459 if penguin is None:
460 penguin = []
461 penguin.append("property of the zoo")
462 return penguin
463
Georg Brandl02c30562007-09-07 17:52:53 +0000464Function call semantics are described in more detail in section :ref:`calls`. A
Georg Brandl116aa622007-08-15 14:28:22 +0000465function call always assigns values to all parameters mentioned in the parameter
466list, either from position arguments, from keyword arguments, or from default
467values. If the form "``*identifier``" is present, it is initialized to a tuple
468receiving any excess positional parameters, defaulting to the empty tuple. If
469the form "``**identifier``" is present, it is initialized to a new dictionary
470receiving any excess keyword arguments, defaulting to a new empty dictionary.
471Parameters after "``*``" or "``*identifier``" are keyword-only parameters and
472may only be passed used keyword arguments.
473
474.. index:: pair: function; annotations
475
476Parameters may have annotations of the form "``: expression``" following the
Georg Brandl02c30562007-09-07 17:52:53 +0000477parameter name. Any parameter may have an annotation even those of the form
478``*identifier`` or ``**identifier``. Functions may have "return" annotation of
479the form "``-> expression``" after the parameter list. These annotations can be
Georg Brandl116aa622007-08-15 14:28:22 +0000480any valid Python expression and are evaluated when the function definition is
Georg Brandl02c30562007-09-07 17:52:53 +0000481executed. Annotations may be evaluated in a different order than they appear in
482the source code. The presence of annotations does not change the semantics of a
483function. The annotation values are available as values of a dictionary keyed
Georg Brandl116aa622007-08-15 14:28:22 +0000484by the parameters' names in the :attr:`__annotations__` attribute of the
485function object.
486
487.. index:: pair: lambda; form
488
489It is also possible to create anonymous functions (functions not bound to a
490name), for immediate use in expressions. This uses lambda forms, described in
491section :ref:`lambda`. Note that the lambda form is merely a shorthand for a
492simplified function definition; a function defined in a ":keyword:`def`"
493statement can be passed around or assigned to another name just like a function
494defined by a lambda form. The ":keyword:`def`" form is actually more powerful
495since it allows the execution of multiple statements and annotations.
496
497**Programmer's note:** Functions are first-class objects. A "``def``" form
498executed inside a function definition defines a local function that can be
499returned or passed around. Free variables used in the nested function can
500access the local variables of the function containing the def. See section
501:ref:`naming` for details.
502
503
504.. _class:
505
506Class definitions
507=================
508
509.. index::
510 pair: class; definition
511 statement: class
Georg Brandl02c30562007-09-07 17:52:53 +0000512 object: class
Georg Brandl116aa622007-08-15 14:28:22 +0000513 single: inheritance
514 pair: class; name
515 pair: name; binding
516 pair: execution; frame
517
Georg Brandl02c30562007-09-07 17:52:53 +0000518A class definition defines a class object (see section :ref:`types`):
519
520.. XXX need to document PEP 3115 changes here (new metaclasses)
521
522.. productionlist::
523 classdef: [`decorators`] "class" `classname` [`inheritance`] ":" `suite`
524 inheritance: "(" [`expression_list`] ")"
525 classname: `identifier`
526
527
Georg Brandl116aa622007-08-15 14:28:22 +0000528A class definition is an executable statement. It first evaluates the
529inheritance list, if present. Each item in the inheritance list should evaluate
530to a class object or class type which allows subclassing. The class's suite is
531then executed in a new execution frame (see section :ref:`naming`), using a
532newly created local namespace and the original global namespace. (Usually, the
533suite contains only function definitions.) When the class's suite finishes
534execution, its execution frame is discarded but its local namespace is saved. A
535class object is then created using the inheritance list for the base classes and
536the saved local namespace for the attribute dictionary. The class name is bound
537to this class object in the original local namespace.
538
Georg Brandl02c30562007-09-07 17:52:53 +0000539Classes can also be decorated; as with functions, ::
540
541 @f1(arg)
542 @f2
543 class Foo: pass
544
545is equivalent to ::
546
547 class Foo: pass
548 Foo = f1(arg)(f2(Foo))
549
Georg Brandl116aa622007-08-15 14:28:22 +0000550**Programmer's note:** Variables defined in the class definition are class
551variables; they are shared by all instances. To define instance variables, they
552must be given a value in the :meth:`__init__` method or in another method. Both
553class and instance variables are accessible through the notation
554"``self.name``", and an instance variable hides a class variable with the same
555name when accessed in this way. Class variables with immutable values can be
Georg Brandl85eb8c12007-08-31 16:33:38 +0000556used as defaults for instance variables. Descriptors can be used to create
557instance variables with different implementation details.
558
559.. XXX add link to descriptor docs above
Georg Brandl116aa622007-08-15 14:28:22 +0000560
Georg Brandl02c30562007-09-07 17:52:53 +0000561.. seealso::
562
563 :pep:`3129` - Class Decorators
564
565
566
Georg Brandl116aa622007-08-15 14:28:22 +0000567.. rubric:: Footnotes
568
569.. [#] The exception is propogated to the invocation stack only if there is no
570 :keyword:`finally` clause that negates the exception.
571
572.. [#] Currently, control "flows off the end" except in the case of an exception or the
573 execution of a :keyword:`return`, :keyword:`continue`, or :keyword:`break`
574 statement.