weird try/finally behaviour
zutesmog at gmail.com
Fri Apr 10 14:04:09 CEST 2009
You should have a read of the python docs, specifically on try:
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).
On Apr 10, 7:49 pm, Sylvain Thénault <sylvain.thena... at logilab.fr>
> 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()
> 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
More information about the Python-list