Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #64968

Re: Try-except-finally paradox

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'finally:': 0.07; 'except:': 0.09; 'executed': 0.09; 'suppress': 0.09; 'try:': 0.09; 'def': 0.12; 'jan': 0.12; '"finally"': 0.16; 'discussion.': 0.16; 'earlier.': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'later': 0.20; '>>>': 0.22; 'appears': 0.22; 'email addr:gmail.com&gt;': 0.22; 'print': 0.22; '&gt;&gt;&gt;': 0.24; 'specify': 0.24; '&gt;': 0.26; 'header:In-Reply-To:1': 0.27; 'raise': 0.29; 'message-id:@mail.gmail.com': 0.30; 'that.': 0.31; 'raised': 0.31; 'this.': 0.32; 'except': 0.35; 'something': 0.35; 'case,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'false': 0.36; 'skip:& 10': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'rather': 0.38; 'does': 0.39; 'flow': 0.39; 'to:addr:python.org': 0.39; 'most': 0.60; 'finally': 0.65; 'behavior': 0.77
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=/wa1EF4kr+buzEtm7ae5oIW2HDCeGRNy1us5zvMH5+A=; b=YGaWVjyI1sPF544Oiw4OQoXZwgGZGfAa9yv2lA0Mf9JMjyi/v2drvz2TCKZKGUxl59 TnBfnxZGlg0wQTNedJsmnc2HiZOmcGf3G+m9ONxPQJ3U5sYSuLScEfxXM4ezaddbOQrh ZC7J04RkXETyBx+OTVvJNYUkl4h//g2n+5TZC2/3/pVB/DPuFGZwLDAkpf3fhA8WgaHy D2M0yxIcZq3TIAiJjN4TjxTzZcyz4M3jVtGZQjaXFT4qG2MZiJQnFcsFtK0gli7c2Xcg /YkRVkwnU3STuXqO3E24QZuFlenjzFrZx5jDxiahFnFWgmMG1Xkga9lHY/accBAc5rqT zkoA==
MIME-Version 1.0
X-Received by 10.66.41.106 with SMTP id e10mr12531174pal.109.1391063027588; Wed, 29 Jan 2014 22:23:47 -0800 (PST)
In-Reply-To <9314ac52-a2be-4382-94ef-2c291f32be1a@googlegroups.com>
References <9314ac52-a2be-4382-94ef-2c291f32be1a@googlegroups.com>
Date Wed, 29 Jan 2014 23:23:47 -0700
Subject Re: Try-except-finally paradox
From Ian Kelly <ian.g.kelly@gmail.com>
To Python <python-list@python.org>
Content-Type multipart/alternative; boundary=bcaec52e5ca19ea42d04f12a19c1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.6110.1391063030.18130.python-list@python.org> (permalink)
Lines 72
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1391063030 news.xs4all.nl 2942 [2001:888:2000:d::a6]:60579
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:64968

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

On Jan 29, 2014 11:01 PM, "Jessica Ross" <deathweasel@gmail.com> wrote:
>
> I found something like this in a StackOverflow discussion.
> >>> def paradox():
> ...     try:
> ...             raise Exception("Exception raised during try")
> ...     except:
> ...             print "Except after try"
> ...             return True
> ...     finally:
> ...             print "Finally"
> ...             return False
> ...     return None
> ...
> >>> return_val = paradox()
> Except after try
> Finally
> >>> return_val
> False
>
> I understand most of this.
> What I don't understand is why this returns False rather than True. Does
the finally short-circuit the return in the except block?

The docs don't seem to specify what happens in this case, but this behavior
is intuitive to me. If the except suite had raised an exception instead of
returning, the return in the finally would suppress that. The generalized
rule appears to be that the control flow specification executed later
overrides the earlier.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Try-except-finally paradox Jessica Ross <deathweasel@gmail.com> - 2014-01-29 21:56 -0800
  Re: Try-except-finally paradox Ian Kelly <ian.g.kelly@gmail.com> - 2014-01-29 23:23 -0700
  Re: Try-except-finally paradox Andrew Berg <robotsondrugs@gmail.com> - 2014-01-30 00:33 -0600
    Re: Try-except-finally paradox Rotwang <sg552@hotmail.co.uk> - 2014-01-30 18:12 +0000
      Re: Try-except-finally paradox Ethan Furman <ethan@stoneleaf.us> - 2014-01-30 10:30 -0800
  Re: Try-except-finally paradox wxjmfauth@gmail.com - 2014-01-29 22:59 -0800
  Re:Try-except-finally paradox Dave Angel <davea@davea.name> - 2014-01-30 07:05 -0500
  Re: Try-except-finally paradox Chris Angelico <rosuav@gmail.com> - 2014-01-31 00:02 +1100
  Re: Try-except-finally paradox MRAB <python@mrabarnett.plus.com> - 2014-01-30 13:11 +0000
  Re: Try-except-finally paradox Chris Angelico <rosuav@gmail.com> - 2014-01-31 00:19 +1100
  Re: Try-except-finally paradox Terry Reedy <tjreedy@udel.edu> - 2014-01-31 00:26 -0500
  Re: Try-except-finally paradox Göktuğ Kayaalp <self@gkayaalp.com> - 2014-02-01 03:58 +0200

csiph-web