Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4e6688d

Browse files
committed
Issue #21366: Document the fact that return in a finally clause
overrides a ``return`` in the ``try`` suite.
2 parents b8ac3e1 + 8edd532 commit 4e6688d

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

Doc/reference/compound_stmts.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,20 @@ statement, the :keyword:`finally` clause is also executed 'on the way out.' A
337337
reason is a problem with the current implementation --- this restriction may be
338338
lifted in the future).
339339

340+
The return value of a function is determined by the last :keyword:`return`
341+
statement executed. Since the :keyword:`finally` clause always executes, a
342+
:keyword:`return` statement executed in the :keyword:`finally` clause will
343+
always be the last one executed::
344+
345+
>>> def foo():
346+
... try:
347+
... return 'try'
348+
... finally:
349+
... return 'finally'
350+
...
351+
>>> foo()
352+
'finally'
353+
340354
Additional information on exceptions can be found in section :ref:`exceptions`,
341355
and information on using the :keyword:`raise` statement to generate exceptions
342356
may be found in section :ref:`raise`.

0 commit comments

Comments
 (0)