weird try/finally behaviour

  • Thread starter Sylvain Thénault
  • Start date
S

Sylvain Thénault

Hi there,

I've encountered the following behaviour which I found surprising:
.... for x in ('test', 'tests'):
.... try:
.... if True:
.... print 'return'
.... return 1
.... finally:
.... print 'break'
.... break
.... print 'end'
.... 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?
 
T

Tim Hoffman

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
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,141
Latest member
BlissKeto
Top