Frank B <fbick...@gmail.com> Wrote in message:
> Ok; this is a bit esoteric.
> 
> So finally is executed regardless of whether an exception occurs, so states 
> the docs.
> 
> But, I thought, if I <return> from my function first, that should take 
> precedence.
> 
> au contraire
> 
> Turns out that if you do this:
> 
> try:
>   failingthing()
> except FailException:
>   return 0
> finally:
>   return 1
> 
> Then finally really is executed regardless... even though you told it to 
> return.
> 
> That seems odd to me.
> 

The thing that's odd to me is that a return is permissible inside
 a finally block. That return
should be at top level,  even with the finally line. And of course
 something else should be in the body of the finally
 block.

If you wanted the finally block to change the return value,  it
 should do it via a variable.

retval = 0 
try:
     failingthing()
except FailException:
     return retval
finally:
     retval =1 
return something

I imagine the finally clause was designed to do cleanup, like
 closing files.  And it certainly predated the with statement.
 

-- 
DaveA

-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to