Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > try...finally is more powerful than I thought.

Reply
Thread Tools

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

 
 
Brian Kelley
Guest
Posts: n/a
 
      11-06-2003
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

 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      11-06-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
 
Reply With Quote
 
 
 
 
Lee Harr
Guest
Posts: n/a
 
      11-06-2003
>> def res():
>> try:
>> a = 1
>> return
>> finally:
>> print "do I get here?"
>>
>> res()
>>
>> outputs "do I get here?"
>>


> 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



>>> def res():

.... print 1
.... try:
.... print 2
.... return 3
.... print 4
.... finally:
.... print 5
....
>>> res()

1
2
5
3


interesting.

 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      11-07-2003
Lee Harr wrote:
>
> >>> def res():

> ... print 1
> ... try:
> ... print 2
> ... return 3
> ... print 4
> ... finally:
> ... print 5
> ...
> >>> res()

> 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
 
Reply With Quote
 
Michael Hudson
Guest
Posts: n/a
 
      11-07-2003
Brian Kelley <(E-Mail Removed)> writes:

> 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

--
I'm not sure that the ability to create routing diagrams
similar to pretzels with mad cow disease is actually a
marketable skill. -- Steve Levin
-- http://home.xnet.com/~raven/Sysadmin/ASR.Quotes.html
 
Reply With Quote
 
Alan Kennedy
Guest
Posts: n/a
 
      11-07-2003
[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,

--
alan kennedy
-----------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan: http://xhaus.com/mailto/alan
 
Reply With Quote
 
Ype Kingma
Guest
Posts: n/a
 
      11-07-2003

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.p....2&view=markup

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

Have fun,
Ype


email at xs4all.nl
 
Reply With Quote
 
Mauro Cicognini
Guest
Posts: n/a
 
      11-07-2003
Alan Kennedy wrote:

> 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

 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      11-07-2003
Mauro Cicognini wrote:
>
> 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
 
Reply With Quote
 
Georgy Pruss
Guest
Posts: n/a
 
      11-08-2003

"Mauro Cicognini" <(E-Mail Removed)> wrote in message news:bogm9e$bn3$(E-Mail Removed)...
>
> <,,,>
> 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-:

>
> Mauro



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Like all great travelers, I have seen more than I remember andremember more than I have seen. shenrilaa@gmail.com Java 0 03-06-2008 08:11 AM
Like all great travelers, I have seen more than I remember andremember more than I have seen. shenrilaa@gmail.com C++ 0 03-05-2008 08:41 AM
Like all great travelers, I have seen more than I remember andremember more than I have seen. shenrilaa@gmail.com C Programming 0 03-05-2008 03:26 AM
Storm worm botnet more powerful than top supercomputers Jonathan Walker NZ Computing 1 09-08-2007 02:18 AM



Advertisments