From: Zachary Ware Date: Tue, 6 May 2014 14:07:13 +0000 (-0500) Subject: Issue #21366: Document the fact that ``return`` in a ``finally`` clause X-Git-Tag: v2.7.7rc1~27 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1512143ef044386916dabfbd6f218ca05df2d943;p=python Issue #21366: Document the fact that ``return`` in a ``finally`` clause overrides a ``return`` in the ``try`` suite. --- diff --git a/Doc/reference/compound_stmts.rst b/Doc/reference/compound_stmts.rst index 56e0769983..53718615fd 100644 --- a/Doc/reference/compound_stmts.rst +++ b/Doc/reference/compound_stmts.rst @@ -320,6 +320,20 @@ statement, the :keyword:`finally` clause is also executed 'on the way out.' A 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`.