[issue21366] Document that return in finally overwrites prev value
New submission from Jon Brandvein: 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. ---------- assignee: docs@python components: Documentation messages: 217277 nosy: brandjon, docs@python priority: normal severity: normal status: open title: Document that return in finally overwrites prev value type: behavior _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21366> _______________________________________
Éric Araujo added the comment: 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? ---------- nosy: +eric.araujo, r.david.murray _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21366> _______________________________________
R. David Murray added the comment: 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" :) ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21366> _______________________________________
Zachary Ware added the comment: How's this, or is it too much? ---------- keywords: +patch nosy: +zach.ware Added file: http://bugs.python.org/file35154/issue21366.diff _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21366> _______________________________________
Changes by Zachary Ware <zachary.ware@gmail.com>: ---------- stage: -> patch review type: behavior -> enhancement versions: +Python 3.4, Python 3.5 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21366> _______________________________________
R. David Murray added the comment: 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". ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21366> _______________________________________
Roundup Robot added the comment: 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 ---------- nosy: +python-dev _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21366> _______________________________________
Roundup Robot added the comment: 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 ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21366> _______________________________________
Zachary Ware added the comment: 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! ---------- resolution: -> fixed stage: patch review -> resolved status: open -> closed versions: +Python 2.7 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21366> _______________________________________
participants (5)
-
Jon Brandvein
-
R. David Murray
-
Roundup Robot
-
Zachary Ware
-
Éric Araujo