try...finally is more powerful than I thought.

B

Brian Kelley

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 :)

That being said, I like it a lot. How is this working internally? Does
the finally get executed when try code block goes out of scope? This
would happen during a return or an exception which could explain the magic.

Brian
 
P

Peter Hansen

Brian said:
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:
.... try:
.... return 1
.... finally:
.... return 2
........ try:
.... return 1
.... finally:
.... return
........ try:
.... return 1
.... finally:
.... pass
....1

-Peter
 
L

Lee Harr

def res():
These results might also be of interest:

... try:
... return 1
... finally:
... return 2
...
... try:
... return 1
... finally:
... return
...
... try:
... return 1
... finally:
... pass
...
.... print 1
.... try:
.... print 2
.... return 3
.... print 4
.... finally:
.... print 5
....1
2
5
3


interesting.
 
P

Peter Hansen

Lee said:
... print 1
... try:
... print 2
... return 3
... print 4
... finally:
... print 5
...
1
2
5
3

interesting.

Why? It just proves that "finally" works, and
executes when the return statement is encountered.

(The point of my examples was to show that finally can
actually override the value that would otherwise have been
returned. I thought that was interesting, but unfortunately
I'm not sure what your example adds to the previous two posts.
Maybe I'm just missing the point...)

-Peter
 
M

Michael Hudson

Brian Kelley said:
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 :)

That being said, I like it a lot. How is this working internally?
Does the finally get executed when try code block goes out of scope?
This would happen during a return or an exception which could explain
the magic.

Internally, and locally to one function, leaving via returning a value
and raising an exception is pretty similar.

Cheers,
mwh
 
A

Alan Kennedy

[Brian Kelley]
How is [try..finally] working internally? Does
the finally get executed when try code block goes out of scope? This
would happen during a return or an exception which could explain the
magic.

From the Python Language Reference

http://www.python.org/doc/current/ref/try.html

"""
The try...finally form specifies a `cleanup' handler. The try clause
is executed. When no exception occurs, the finally clause is executed.
When an exception occurs in the try clause, the exception is
temporarily saved, the finally clause is executed, and then the saved
exception is re-raised. If the finally clause raises another exception
or executes a return or break statement, the saved exception is lost.
A continue statement is illegal in the finally clause. (The reason is
a problem with the current implementation - this restriction may be
lifted in the future). The exception information is not available to
the program during execution of the finally clause.
"""

The Language Reference is very readable, not excessively formal, and
well worth a read.

http://www.python.org/doc/current/ref/ref.html

regards,
 
Y

Ype Kingma

Jython once had a bug where it would not execute the outer finally
through a return, ie. in:

def x():
try:
try:
return 1
finally:
doSomething1()
finally:
doSomething2()

doSomething2() would not get executed.
The workaround was straightforward: save the return value in some local
variable and return at the end.

In case you like such puzzles, this is the test code:

http://cvs.sourceforge.net/viewcvs.py/jython/bugtests/test371.py?rev=1.2&view=markup

Btw. CPython never failed a single test case in there.

Have fun,
Ype


email at xs4all.nl
 
M

Mauro Cicognini

Alan said:
When an exception occurs in the try clause, the exception is
temporarily saved, the finally clause is executed, and then the saved
exception is re-raised.

I've always wondered, how does one use this in real life?
Since I cannot write try... except... finally..., which would be the
most intuitive construct to me, I understand I should write
try:
try:
...
finally:
...
except:
...

to achieve the same effect.

Am I right? I.e. (again) how do you people use try .. finally in real
use cases?

Does anyone else think Python could have a nicer syntax for this one?

Mauro
 
P

Peter Hansen

Mauro said:
I've always wondered, how does one use this in real life?
Since I cannot write try... except... finally..., which would be the
most intuitive construct to me, I understand I should write
try:
try:
...
finally:
...
except:
...

to achieve the same effect.

Am I right? I.e. (again) how do you people use try .. finally in real
use cases?

I use the above in the exceptionally rare case where I really want
that particular behaviour: cleanup prior to unrelated exception handling.

Normally what I want is cleanup, leaving exception handling to the calling
code, in which case I just need a finally.

More often I want exception handling, and don't need cleanup, so of course
just the except clause will do.

And only slightly more often than the first case, but much less often
than the other two, I want exception handling but an overall cleanup,
in which case I use the above nested format but with the finally on
the outside and the except on the inside.
Does anyone else think Python could have a nicer syntax for this one?

Perhaps, but it's such an incredibly insignificant thing as far as
I'm concerned that after hearing of the existence of lengthy past
discussions about this, and the near certainty it will not be changed,
I stopped wasting my time worrying about it and went back to writing
code that worked, and some that didn't :).

(Basically, check the archives for the reasons this is the way it is.)

-Peter
 
G

Georgy Pruss

Mauro Cicognini said:
<,,,>
try:
try:
...
finally:
...
except:
...

to achieve the same effect.

Am I right? I.e. (again) how do you people use try .. finally in real
use cases?

Does anyone else think Python could have a nicer syntax for this one?

That's probably why they called it begin...rescue in Ruby :)
G-:
 
L

Lee Harr

Why? It just proves that "finally" works, and
executes when the return statement is encountered.

(The point of my examples was to show that finally can
actually override the value that would otherwise have been
returned. I thought that was interesting, but unfortunately
I'm not sure what your example adds to the previous two posts.
Maybe I'm just missing the point...)


Yes. I just found the entire thread interesting. I know that
people who are unfamiliar with the try ... finally syntax can
easily fire up the interpreter and play around with it for
themselves, but I thought the "basic functionality" example
might make the rest of the thread more clear.

I have written a lot of python code and have never used finally.
 
P

Peter Hansen

Lee said:
Yes. I just found the entire thread interesting. I know that
people who are unfamiliar with the try ... finally syntax can
easily fire up the interpreter and play around with it for
themselves, but I thought the "basic functionality" example
might make the rest of the thread more clear.

I have written a lot of python code and have never used finally.

Ah, good point. :)

-Peter
 
B

Bob Gailer

[Brian Kelley]
How is [try..finally] working internally? Does
the finally get executed when try code block goes out of scope? This
would happen during a return or an exception which could explain the
magic.
From the Python Language Reference

http://www.python.org/doc/current/ref/try.html

"""
The try...finally form specifies a `cleanup' handler. The try clause
is executed. When no exception occurs, the finally clause is executed.
When an exception occurs in the try clause, the exception is
temporarily saved, the finally clause is executed, and then the saved
exception is re-raised. If the finally clause raises another exception
or executes a return or break statement, the saved exception is lost.
A continue statement is illegal in the finally clause. (The reason is
a problem with the current implementation - this restriction may be
lifted in the future). The exception information is not available to
the program during execution of the finally clause.
"""

Which is followed by """When a return, break or continue statement is
executed in the try suite of a try...finally statement, the finally clause
is also executed `on the way out.' A continue statement is illegal in the
finally clause. (The reason is a problem with the current implementation --
this restriction may be lifted in the future).""" which is what applies to
the examples cited above.

Bob Gailer
(e-mail address removed)
303 442 2625
 
M

Mauro Cicognini

Peter said:
Perhaps, but it's such an incredibly insignificant thing as far as
I'm concerned that after hearing of the existence of lengthy past
discussions about this, and the near certainty it will not be changed,
I stopped wasting my time worrying about it and went back to writing
code that worked, and some that didn't :).

(Basically, check the archives for the reasons this is the way it is.)

Thanx for the nice explanation.

I might check the archives... and yes, I agree it is a rather
insignificant thing, in fact I was just curious and the mention of past
wars does check out. I for no reason will ever wish to stir them up again.

BR,
Mauro
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top