This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Document that return in finally overwrites prev value
Type: enhancement Stage: resolved
Components: Documentation Versions: Python 3.4, Python 3.5, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: brandjon, docs@python, eric.araujo, python-dev, r.david.murray, zach.ware
Priority: normal Keywords: patch

Created on 2014-04-27 16:12 by brandjon, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue21366.diff zach.ware, 2014-05-05 15:17 review
Messages (8)
msg217277 - (view) Author: Jon Brandvein (brandjon) Date: 2014-04-27 16:12
def foo():
    try:
      return 1
    finally;
      return 2
  print(foo())  # 2

I've seen this peculiar case discussed on a few blogs lately, but was unable to find confirmation that this behavior is defined.

In the try/finally section of Doc/reference/compound_stmts.rst, immediately after the sentence beginning

> When a return, break, or continue statement is executed

I propose adding something to the effect of:

> A return statement in a finally clause overrides the value of any return statement executed in the try suite.

This wording also handles the case of nested try/finally blocks.
msg217772 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2014-05-02 18:07
Thanks, your proposed wording sounds good to me.  David (picked you seni-randomly as a senior core dev and native speaker), what do you think?
msg217775 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-05-02 18:45
The precisionist in me isn't quite happy, but any wording I've come up with to make it more precise isn't as informative :)  (The 'real' situation is that the return value of the function is determined by the last return statement executed, which in turn is determined by the try/finally logic....but as far as I can see the suggested phrasing is equivalent to that in every respect that matters).

I also have an impulse to add "so don't do that" :)
msg217928 - (view) Author: Zachary Ware (zach.ware) * (Python committer) Date: 2014-05-05 15:17
How's this, or is it too much?
msg217929 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-05-05 15:44
I would remove the parenthetical (it was so stated in immediately preceding paragraph and thus is redundant), and instead of "win", I would say "will always be the last one executed".
msg217984 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-05-06 14:08
New changeset faaa8d569664 by Zachary Ware in branch '3.4':
Issue #21366: Document the fact that ``return`` in a ``finally`` clause
http://hg.python.org/cpython/rev/faaa8d569664

New changeset 685f92aad1dc by Zachary Ware in branch 'default':
Issue #21366: Document the fact that ``return`` in a ``finally`` clause
http://hg.python.org/cpython/rev/685f92aad1dc
msg217985 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-05-06 14:10
New changeset 7fabe3652ee0 by Zachary Ware in branch '2.7':
Issue #21366: Document the fact that ``return`` in a ``finally`` clause
http://hg.python.org/cpython/rev/7fabe3652ee0
msg217986 - (view) Author: Zachary Ware (zach.ware) * (Python committer) Date: 2014-05-06 14:12
Done. Thanks pointing out just how redundant that parenthetical was (I don't know how that slipped through my brain...), David.  And thank you Jon for the report!
History
Date User Action Args
2022-04-11 14:58:02adminsetgithub: 65565
2014-05-06 14:12:14zach.waresetstatus: open -> closed
versions: + Python 2.7
messages: + msg217986

resolution: fixed
stage: patch review -> resolved
2014-05-06 14:10:28python-devsetmessages: + msg217985
2014-05-06 14:08:21python-devsetnosy: + python-dev
messages: + msg217984
2014-05-05 15:44:40r.david.murraysetmessages: + msg217929
2014-05-05 15:18:34zach.waresetstage: patch review
type: behavior -> enhancement
versions: + Python 3.4, Python 3.5
2014-05-05 15:17:51zach.waresetfiles: + issue21366.diff

nosy: + zach.ware
messages: + msg217928

keywords: + patch
2014-05-02 18:45:35r.david.murraysetmessages: + msg217775
2014-05-02 18:07:51eric.araujosetnosy: + eric.araujo, r.david.murray
messages: + msg217772
2014-04-27 16:12:19brandjoncreate