Try-except-finally paradox

Rotwang sg552 at hotmail.co.uk
Thu Jan 30 13:12:38 EST 2014


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



More information about the Python-list mailing list