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


Groups > comp.lang.python > #64969

Re: Try-except-finally paradox

Path csiph.com!usenet.pasdenom.info!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <robotsondrugs@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'cpython': 0.05; 'interpreter': 0.05; 'true,': 0.05; 'finally:': 0.07; 'except:': 0.09; 'false.': 0.09; 'jessica': 0.09; 'try:': 0.09; 'def': 0.12; 'changes': 0.15; 'windows': 0.15; '"finally"': 0.16; 'discussion.': 0.16; 'skipped': 0.16; 'wrote:': 0.18; 'print': 0.22; 'header:User-Agent:1': 0.23; 'gets': 0.27; 'header:In-Reply- To:1': 0.27; 'raise': 0.29; "doesn't": 0.30; 'received:10.0.0': 0.31; '>>>>': 0.31; 'forces': 0.31; 'raised': 0.31; 'this.': 0.32; 'run': 0.32; 'guess': 0.33; 'except': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'false': 0.36; 'received:10.0': 0.36; 'received:10': 0.37; 'message- id:@gmail.com': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'most': 0.60; 'finally': 0.65; 'freebsd': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=WfHNe3lGKsYyVfAlOdSeP2GTW6IkqL6YT+LTbHZUAHE=; b=muSv4IgIkBB0tB5KEV76JmA1hpQOA+juNZIf3u5UqXpsIiwZef3w0aFtblGF0owXiD O1L9ey36msETIkQoeLWGfhswOHlGJHTCUoB0SM2od9tt5c9QJKuQNjEOCBFce84Vagrz H4TrMpxhgna/Y2G92egPK6DWG5451KrLjvwbPNUeOikA/pwlBzgZH6WuX8BqVTjOtNxQ h1/Np3NTkZ3630JFh5As97kLe5Kh8AyBaOkMEHVJ4xovjVU+18N7SkBjDB0KZw+ko7la qcQ7HwOLiaYM4SbKQ26ppVbH7qdudFAPddXWP99hzvd8YxbOmHdA7TJTUDGkPnXWSHBl o1sA==
X-Received by 10.42.96.196 with SMTP id k4mr784707icn.5.1391063623008; Wed, 29 Jan 2014 22:33:43 -0800 (PST)
Date Thu, 30 Jan 2014 00:33:44 -0600
From Andrew Berg <robotsondrugs@gmail.com>
User-Agent Mozilla/5.0 (Windows NT 6.2; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0
MIME-Version 1.0
To python-list@python.org
Subject Re: Try-except-finally paradox
References <9314ac52-a2be-4382-94ef-2c291f32be1a@googlegroups.com>
In-Reply-To <9314ac52-a2be-4382-94ef-2c291f32be1a@googlegroups.com>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 7bit
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.6111.1391063632.18130.python-list@python.org> (permalink)
Lines 27
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1391063632 news.xs4all.nl 2836 [2001:888:2000:d::a6]:36686
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:64969

Show key headers only | View raw


On 2014.01.29 23:56, Jessica Ross 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?
> 
My guess would be that the interpreter doesn't let the finally block get skipped under any circumstances, so the return value gets set to
True, but then it forces the finally block to be run before returning, which changes the return value to False.

-- 
CPython 3.3.2 | Windows NT 6.2.9200 / FreeBSD 10.0

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