Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > weird try/finally behaviour

Reply
Thread Tools

weird try/finally behaviour

 
 
Sylvain Thénault
Guest
Posts: n/a
 
      04-10-2009
Hi there,

I've encountered the following behaviour which I found surprising:

>>> def test():

.... for x in ('test', 'tests'):
.... try:
.... if True:
.... print 'return'
.... return 1
.... finally:
.... print 'break'
.... break
.... print 'end'
....
>>>
>>> test()

return
break
end

As you can see, the 'break' in the finally block makes the 'return 1' beeing ignored.
Is this a known caveat or should it be considered as a bug?
--
Sylvain Thénault LOGILAB, Paris (France)
Formations Python, Debian, Méth. Agiles: http://www.logilab.fr/formations
Développement logiciel sur mesure: http://www.logilab.fr/services
CubicWeb, the semantic web framework: http://www.cubicweb.org

 
Reply With Quote
 
 
 
 
Tim Hoffman
Guest
Posts: n/a
 
      04-10-2009
Hi Sylvain

You should have a read of the python docs, specifically on try:
finally:

excerpt from docs. --

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

See ya

Tim


On Apr 10, 7:49*pm, Sylvain Thnault <(E-Mail Removed)>
wrote:
> Hi there,
>
> I've encountered the following behaviour which I found surprising:
>
> >>> def test():

>
> ... * * for x in ('test', 'tests'):
> ... * * * * *try:
> ... * * * * * * if True:
> ... * * * * * * * * * print 'return'
> ... * * * * * * * * * return 1
> ... * * * * *finally:
> ... * * * * * * print 'break'
> ... * * * * * * break
> ... * * print 'end'
> ...
>
> >>> test()

>
> return
> break
> end
>
> As you can see, the 'break' in the finally block makes the 'return 1' beeing ignored.
> Is this a known caveat or should it be considered as a bug?
> --
> Sylvain Thnault * * * * * * * * * * * * * * * LOGILAB, Paris (France)
> Formations Python, Debian, Mth. Agiles:http://www.logilab.fr/formations
> Dveloppement logiciel sur mesure: * * *http://www.logilab.fr/services
> CubicWeb, the semantic web framework: * *http://www.cubicweb.org


 
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
debugger behaviour different to execution behaviour Andy Chambers Java 1 05-14-2007 09:51 AM
Weird TextBox behaviour fd123456 ASP .Net 1 12-23-2004 04:37 PM
weird user control behaviour - please help! =?Utf-8?B?RGFuIE5hc2g=?= ASP .Net 0 11-02-2004 02:42 PM
Weird behaviour of SetAuthCookie, bug? Strange Cat ASP .Net 1 03-01-2004 04:10 PM
FormsAuthentication - Weird Behaviour dotCore ASP .Net 1 02-09-2004 03:52 AM



Advertisments