diff options
author | Georg Brandl <georg@python.org> | 2008-01-05 19:29:45 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2008-01-05 19:29:45 (GMT) |
commit | 6265833d91b53aec0d710d81ec91cee142b0e204 (patch) | |
tree | 1f63195180a8896940ff3aabc9c930790bb0c120 /Doc/reference/compound_stmts.rst | |
parent | 9749e15e2ff9fffd987679cc3914c726af8876b3 (diff) | |
download | cpython-6265833d91b53aec0d710d81ec91cee142b0e204.zip cpython-6265833d91b53aec0d710d81ec91cee142b0e204.tar.gz cpython-6265833d91b53aec0d710d81ec91cee142b0e204.tar.bz2 |
Simplify index entries; fix #1712.
Diffstat (limited to 'Doc/reference/compound_stmts.rst')
-rw-r--r-- | Doc/reference/compound_stmts.rst | 78 |
1 files changed, 31 insertions, 47 deletions
diff --git a/Doc/reference/compound_stmts.rst b/Doc/reference/compound_stmts.rst index e37618a..02ce783 100644 --- a/Doc/reference/compound_stmts.rst +++ b/Doc/reference/compound_stmts.rst @@ -76,7 +76,10 @@ on a separate line for clarity. The :keyword:`if` statement =========================== -.. index:: statement: if +.. index:: + statement: if + keyword: elif + keyword: else The :keyword:`if` statement is used for conditional execution: @@ -85,10 +88,6 @@ The :keyword:`if` statement is used for conditional execution: : ( "elif" `expression` ":" `suite` )* : ["else" ":" `suite`] -.. index:: - keyword: elif - keyword: else - It selects exactly one of the suites by evaluating the expressions one by one until one is found to be true (see section :ref:`booleans` for the definition of true and false); then that suite is executed (and no other part of the @@ -104,6 +103,7 @@ The :keyword:`while` statement .. index:: statement: while pair: loop; statement + keyword: else The :keyword:`while` statement is used for repeated execution as long as an expression is true: @@ -112,8 +112,6 @@ expression is true: while_stmt: "while" `expression` ":" `suite` : ["else" ":" `suite`] -.. index:: keyword: else - This repeatedly tests the expression and, if it is true, executes the first suite; if the expression is false (which may be the first time it is tested) the suite of the :keyword:`else` clause, if present, is executed and the loop @@ -137,8 +135,10 @@ The :keyword:`for` statement .. index:: statement: for pair: loop; statement - -.. index:: object: sequence + keyword: in + keyword: else + pair: target; list + object: sequence The :keyword:`for` statement is used to iterate over the elements of a sequence (such as a string, tuple or list) or other iterable object: @@ -147,11 +147,6 @@ The :keyword:`for` statement is used to iterate over the elements of a sequence for_stmt: "for" `target_list` "in" `expression_list` ":" `suite` : ["else" ":" `suite`] -.. index:: - keyword: in - keyword: else - pair: target; list - The expression list is evaluated once; it should yield an iterable object. An iterator is created for the result of the ``expression_list``. The suite is then executed once for each item provided by the iterator, in the order of @@ -214,7 +209,10 @@ effect of Pascal's ``for i := a to b do``; e.g., ``range(3)`` returns the list The :keyword:`try` statement ============================ -.. index:: statement: try +.. index:: + statement: try + keyword: except + keyword: finally The :keyword:`try` statement specifies exception handlers and/or cleanup code for a group of statements: @@ -233,8 +231,6 @@ for a group of statements: :keyword:`finally` did not work. :keyword:`try`...\ :keyword:`except` had to be nested in :keyword:`try`...\ :keyword:`finally`. -.. index:: keyword: except - The :keyword:`except` clause(s) specify one or more exception handlers. When no exception occurs in the :keyword:`try` clause, no exception handler is executed. When an exception occurs in the :keyword:`try` suite, a search for an exception @@ -376,12 +372,8 @@ The execution of the :keyword:`with` statement proceeds as follows: .. note:: In Python 2.5, the :keyword:`with` statement is only allowed when the - ``with_statement`` feature has been enabled. It will always be enabled in - Python 2.6. This ``__future__`` import statement can be used to enable the - feature:: - - from __future__ import with_statement - + ``with_statement`` feature has been enabled. It is always enabled in + Python 2.6. .. seealso:: @@ -397,10 +389,10 @@ Function definitions ==================== .. index:: - pair: function; definition statement: def - -.. index:: + pair: function; definition + pair: function; name + pair: name; binding object: user-defined function object: function @@ -421,10 +413,6 @@ A function definition defines a user-defined function object (see section parameter: `identifier` | "(" `sublist` ")" funcname: `identifier` -.. index:: - pair: function; name - pair: name; binding - A function definition is an executable statement. Its execution binds the function name in the current local namespace to a function object (a wrapper around the executable code for the function). This function object contains a @@ -505,10 +493,13 @@ Class definitions ================= .. index:: - pair: class; definition + object: class statement: class - -.. index:: object: class + pair: class; definition + pair: class; name + pair: name; binding + pair: execution; frame + single: inheritance A class definition defines a class object (see section :ref:`types`): @@ -517,12 +508,6 @@ A class definition defines a class object (see section :ref:`types`): inheritance: "(" [`expression_list`] ")" classname: `identifier` -.. index:: - single: inheritance - pair: class; name - pair: name; binding - pair: execution; frame - A class definition is an executable statement. It first evaluates the inheritance list, if present. Each item in the inheritance list should evaluate to a class object or class type which allows subclassing. The class's suite is @@ -535,13 +520,13 @@ the saved local namespace for the attribute dictionary. The class name is bound to this class object in the original local namespace. **Programmer's note:** Variables defined in the class definition are class -variables; they are shared by all instances. To define instance variables, they -must be given a value in the :meth:`__init__` method or in another method. Both -class and instance variables are accessible through the notation -"``self.name``", and an instance variable hides a class variable with the same -name when accessed in this way. Class variables with immutable values can be -used as defaults for instance variables. For :term:`new-style class`\es, -descriptors can be used to create instance variables with different +variables; they are shared by all instances. To create instance variables, they +can be set in a method with ``self.name = value``. Both class and instance +variables are accessible through the notation "``self.name``", and an instance +variable hides a class variable with the same name when accessed in this way. +Class variables can be used as defaults for instance variables, but using +mutable values there can lead to unexpected results. For :term:`new-style +class`\es, descriptors can be used to create instance variables with different implementation details. .. rubric:: Footnotes @@ -552,4 +537,3 @@ implementation details. .. [#] Currently, control "flows off the end" except in the case of an exception or the execution of a :keyword:`return`, :keyword:`continue`, or :keyword:`break` statement. - |