|
Original |
Translation |
|
20
|
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 ascending indices. Each item in turn is assigned to the target list using the standard rules for assignments (see :ref:`assignment`), and then the suite is executed. When the items are exhausted (which is immediately when the sequence is empty or an iterator raises a :exc:`StopIteration` exception), the suite in the :keyword:`else` clause, if present, is executed, and the loop terminates.
|
|
|
21
|
A :keyword:`break` statement executed in the first suite terminates the loop without executing the :keyword:`else` clause's suite. A :keyword:`continue` statement executed in the first suite skips the rest of the suite and continues with the next item, or with the :keyword:`else` clause if there was no next item.
|
|
|
22
|
The suite may assign to the variable(s) in the target list; this does not affect the next item assigned to it.
|
|
|
23
|
Names in the target list are not deleted when the loop is finished, but if the sequence is empty, it will not have been assigned to at all by the loop. Hint: the built-in function :func:`range` returns an iterator of integers suitable to emulate the effect of Pascal's ``for i := a to b do``; e.g., ``list(range(3))`` returns the list ``[0, 1, 2]``.
|
|
|
24
|
|
25
|
for x in a[:]: if x < 0: a.remove(x)
|
|
|
26
|
The :keyword:`try` statement
|
|
|
27
|
The :keyword:`try` statement specifies exception handlers and/or cleanup code for a group of statements:
|
|
|
28
|
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 handler is started. This search inspects the except clauses in turn until one is found that matches the exception. An expression-less except clause, if present, must be last; it matches any exception. For an except clause with an expression, that expression is evaluated, and the clause matches the exception if the resulting object is "compatible" with the exception. An object is compatible with an exception if it is the class or a base class of the exception object or a tuple containing an item compatible with the exception.
|
|
|
29
|
If no except clause matches the exception, the search for an exception handler continues in the surrounding code and on the invocation stack. [#]_
|
|