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~
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to