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


Groups > comp.lang.python > #65036

Re: Try-except-finally paradox

Path csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ethan@stoneleaf.us>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'interpreter': 0.05; 'true,': 0.05; 'finally:': 0.07; 'except:': 0.09; 'false.': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'jessica': 0.09; 'message- id:@stoneleaf.us': 0.09; 'try:': 0.09; '~ethan~': 0.09; 'def': 0.12; 'changes': 0.15; '"finally"': 0.16; 'block:': 0.16; 'discussion.': 0.16; 'evaluating': 0.16; 'skipped': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'raise': 0.29; "doesn't": 0.30; 'andrew': 0.30; 'too.': 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; 'false': 0.36; 'charset:us-ascii': 0.36; 'mine': 0.38; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'most': 0.60; 'received:173': 0.61; 'finally': 0.65; 'thorough,': 0.84
Date Thu, 30 Jan 2014 10:30:21 -0800
From Ethan Furman <ethan@stoneleaf.us>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121010 Thunderbird/16.0.1
MIME-Version 1.0
To python-list@python.org
Subject Re: Try-except-finally paradox
References <9314ac52-a2be-4382-94ef-2c291f32be1a@googlegroups.com> <mailman.6111.1391063632.18130.python-list@python.org> <lce4mr$p4b$1@dont-email.me>
In-Reply-To <lce4mr$p4b$1@dont-email.me>
Content-Type text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding 7bit
X-AntiAbuse This header was added to track abuse, please include it with any abuse report
X-AntiAbuse Primary Hostname - gator3304.hostgator.com
X-AntiAbuse Original Domain - python.org
X-AntiAbuse Originator/Caller UID/GID - [47 12] / [47 12]
X-AntiAbuse Sender Address Domain - stoneleaf.us
X-BWhitelist no
X-Source-IP 173.12.184.233
X-Source
X-Source-Args
X-Source-Dir
X-Source-Sender ([173.12.184.233]) [173.12.184.233]:60669
X-Source-Auth ethan+stoneleaf.us
X-Email-Count 1
X-Source-Cap dG9idWs7dG9idWs7Z2F0b3IzMzA0Lmhvc3RnYXRvci5jb20=
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.6157.1391107943.18130.python-list@python.org> (permalink)
Lines 71
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1391107944 news.xs4all.nl 2851 [2001:888:2000:d::a6]:47601
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:65036

Show key headers only | View raw


On 01/30/2014 10:12 AM, Rotwang wrote:
> On 30/01/2014 06:33, Andrew Berg wrote:
>> 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.
>
> Mine too. We can check that the interpreter gets as far as evaluating the return value in the except block:
>
> --> def paradox2():
>        try:
>            raise Exception("Raise")
>        except:
>            print("Except")
>            return [print("Return"), True][1]
>        finally:
>            print("Finally")
>            return False
>        return None
>
> --> ret = paradox2()
> Except
> Return
> Finally
> --> ret
> False

And just to be thorough, if the finally block doesn't have a return:

--> def paradox3():
         try:
             raise Exception("Raise")
         except:
             print("Except")
             return [print("Return"), True][1]
         finally:
             print("Finally")
         return None

--> print(paradox3())
Except
Return
Finally
True

--
~Ethan~

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