try...finally is more powerful than I thought.
Peter Hansen
peter at engcorp.com
Thu Nov 6 16:26:32 EST 2003
Brian Kelley wrote:
>
> def res():
> try:
> a = 1
> return
> finally:
> print "do I get here?"
>
> res()
>
> outputs "do I get here?"
>
> I can't say why I didn't really expect this, the control flow is a
> little wierd as the function isn't really returning at the "return"
> statement but executing the bit in the finally: block and then
> returning. I think :)
These results might also be of interest:
>>> def res():
... try:
... return 1
... finally:
... return 2
...
>>> res()
2
>>> def res():
... try:
... return 1
... finally:
... return
...
>>> res()
>>> def res():
... try:
... return 1
... finally:
... pass
...
>>> res()
1
-Peter
More information about the Python-list
mailing list