Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #65032
| From | Rotwang <sg552@hotmail.co.uk> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Try-except-finally paradox |
| Date | 2014-01-30 18:12 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <lce4mr$p4b$1@dont-email.me> (permalink) |
| References | <9314ac52-a2be-4382-94ef-2c291f32be1a@googlegroups.com> <mailman.6111.1391063632.18130.python-list@python.org> |
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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