[Python-checkins] cpython (merge 3.4 -> default): Issue #21366: Document the fact that ``return`` in a ``finally`` clause

zach.ware python-checkins at python.org
Tue May 6 16:08:23 CEST 2014


http://hg.python.org/cpython/rev/685f92aad1dc
changeset:   90569:685f92aad1dc
parent:      90567:2b18f6c37a68
parent:      90568:faaa8d569664
user:        Zachary Ware <zachary.ware at gmail.com>
date:        Tue May 06 09:07:51 2014 -0500
summary:
  Issue #21366: Document the fact that ``return`` in a ``finally`` clause
overrides a ``return`` in the ``try`` suite.

files:
  Doc/reference/compound_stmts.rst |  14 ++++++++++++++
  1 files changed, 14 insertions(+), 0 deletions(-)


diff --git a/Doc/reference/compound_stmts.rst b/Doc/reference/compound_stmts.rst
--- a/Doc/reference/compound_stmts.rst
+++ b/Doc/reference/compound_stmts.rst
@@ -337,6 +337,20 @@
 reason is a problem with the current implementation --- this restriction may be
 lifted in the future).
 
+The return value of a function is determined by the last :keyword:`return`
+statement executed.  Since the :keyword:`finally` clause always executes, a
+:keyword:`return` statement executed in the :keyword:`finally` clause will
+always be the last one executed::
+
+   >>> def foo():
+   ...     try:
+   ...         return 'try'
+   ...     finally:
+   ...         return 'finally'
+   ...
+   >>> foo()
+   'finally'
+
 Additional information on exceptions can be found in section :ref:`exceptions`,
 and information on using the :keyword:`raise` statement to generate exceptions
 may be found in section :ref:`raise`.

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list