
Spotted on c.l.python. Although Pythonwin is mentioned, python.exe gives the same results - as does 1.5.2. Seems a reasonable question... [Also, if Robin hasn't been invited to join us here, I think it could make some sense...] Mark. -------- Original Message -------- Subject: try...else Date: Fri, 22 Dec 2000 18:02:27 +0000 From: Robin Becker <robin@jessikat.fsnet.co.uk> Newsgroups: comp.lang.python I had expected that in try: except: else the else clause always got executed, but it seems not for return PythonWin 2.0 (#8, Oct 16 2000, 17:27:58) [MSC 32 bit (Intel)] on win32.Portions Copyright 1994-2000 Mark Hammond (MarkH@ActiveState.com) - see 'Help/About PythonWin' for further copyright information.
is this a 'feature' or bug. The 2.0 docs seem not to mention return/continue except for try finally. -- Robin Becker

Mark Hammond wrote:
I think Robin mixed up try...finally with try...except...else. The finally clause is executed even in case an exception occurred. He does have a point however that 'return' will bypass try...else and try...finally clauses. I don't think we can change that behaviour, though, as it would break code.
-- Marc-Andre Lemburg ______________________________________________________________________ Company: http://www.egenix.com/ Consulting: http://www.lemburg.com/ Python Pages: http://www.lemburg.com/python/

Moshe Zadka wrote:
Hmm, that must have changed between Python 1.5 and more recent versions: Python 1.5:
Python 2.0:
-- Marc-Andre Lemburg ______________________________________________________________________ Company: http://www.egenix.com/ Consulting: http://www.lemburg.com/ Python Pages: http://www.lemburg.com/python/

Moshe Zadka wrote:
Sorry, false alarm: there was a bug in my patched 1.5 version. The original 1.5 version does not show the described behaviour. -- Marc-Andre Lemburg ______________________________________________________________________ Company: http://www.egenix.com/ Consulting: http://www.lemburg.com/ Python Pages: http://www.lemburg.com/python/

On Thu, 28 Dec 2000, M.-A. Lemburg wrote:
I think Robin mixed up try...finally with try...except...else.
I think so too.
return does not skip finally clauses[1]. In my not especially humble opinion, the current behaviour is the Right Thing. I'd have to think for a moment before saying what Robin's example would print, but I think the alternative would disturb me far more. Cheers, M. [1] In fact the flow of control on return is very similar to that of an exception - ooh, look at that implementation...

On Thu, Dec 28, 2000 at 03:45:49PM +0100, M.-A. Lemburg wrote:
I had expected that in try: except: else the else clause always got executed, but it seems not for return
I think Robin mixed up try...finally with try...except...else. The finally clause is executed even in case an exception occurred.
(MAL and I already discussed this in private mail: Robin did mean try/except/else, and 'finally' already executes when returning directly from the 'try' block, even in Python 1.5)
This code: try: return except: pass else: print "returning" will indeed not print 'returning', but I believe it's by design. I'm against changing it, in any case, and not just because it'd break code :) If you want something that always executes, use a 'finally'. Or don't return from the 'try', but return in the 'else' clause. The 'except' clause is documented to execute if a matching exception occurs, and 'else' if no exception occurs. Maybe the intent of the 'else' clause would be clearer if it was documented to 'execute if the try: clause finishes without an exception being raised' ? The 'else' clause isn't executed when you 'break' or (after applying my continue-in-try patch ;) 'continue' out of the 'try', either. Robin... Did I already reply this, on python-list or to you directly ? I distinctly remember writing that post, but I'm not sure if it arrived. Maybe I didn't send it after all, or maybe something on mail.python.org is detaining it ? -- Thomas Wouters <thomas@xs4all.net> Hi! I'm a .signature virus! copy me into your .signature file to help me spread!

On Thu, 28 Dec 2000 21:21:15 +0100, Thomas Wouters <thomas@xs4all.net> wrote:
This can certainly be clarified in the documentation -- please file a bug report at http://sourceforge.net/projects/python/. Thanks! -Fred -- Fred L. Drake, Jr. <fdrake at acm.org> PythonLabs at Digital Creations

[Fred]
This can certainly be clarified in the documentation -- please file a bug report at http://sourceforge.net/projects/python/.
Here you go: https://sourceforge.net/bugs/?func=detailbug&bug_id=127098&group_id=5470

[Fred, suggested doc change near the end] [Thomas Wouters]
Guido's out of town again, so I'll channel him: Thomas is correct on all counts. In try/else, the "else" clause should execute if and only if control "falls off the end" of the "try" block. IOW, consider: try: arbitrary stuff x = 1 An "else" clause added to that "try" should execute when and only when the code as written executes the "x = 1" after the block. When "arbitrary stuff" == "return", control does not fall off the end, so "else" shouldn't trigger. Same thing if "arbitrary stuff" == "break" and we're inside a loop, or "continue" after Thomas's patch gets accepted.
The 'except' clause is documented to execute if a matching exception occurs, and 'else' if no exception occurs.
Yup, and that's imprecise: the same words are used to describe (part of) when 'finally' executes, but they weren't intended to be the same.
Sorry, I don't find that any clearer. Let's be explicit: The optional 'else' clause is executed when the 'try' clause terminates by any means other than an exception or executing a 'return', 'continue' or 'break' statement. Exceptions in the 'else' clause are not handled by the preceding 'except' clauses.
Hey, now you're channeling me <wink>! Be afraid -- be very afraid.

Mark Hammond wrote:
I think Robin mixed up try...finally with try...except...else. The finally clause is executed even in case an exception occurred. He does have a point however that 'return' will bypass try...else and try...finally clauses. I don't think we can change that behaviour, though, as it would break code.
-- Marc-Andre Lemburg ______________________________________________________________________ Company: http://www.egenix.com/ Consulting: http://www.lemburg.com/ Python Pages: http://www.lemburg.com/python/

Moshe Zadka wrote:
Hmm, that must have changed between Python 1.5 and more recent versions: Python 1.5:
Python 2.0:
-- Marc-Andre Lemburg ______________________________________________________________________ Company: http://www.egenix.com/ Consulting: http://www.lemburg.com/ Python Pages: http://www.lemburg.com/python/

Moshe Zadka wrote:
Sorry, false alarm: there was a bug in my patched 1.5 version. The original 1.5 version does not show the described behaviour. -- Marc-Andre Lemburg ______________________________________________________________________ Company: http://www.egenix.com/ Consulting: http://www.lemburg.com/ Python Pages: http://www.lemburg.com/python/

On Thu, 28 Dec 2000, M.-A. Lemburg wrote:
I think Robin mixed up try...finally with try...except...else.
I think so too.
return does not skip finally clauses[1]. In my not especially humble opinion, the current behaviour is the Right Thing. I'd have to think for a moment before saying what Robin's example would print, but I think the alternative would disturb me far more. Cheers, M. [1] In fact the flow of control on return is very similar to that of an exception - ooh, look at that implementation...

On Thu, Dec 28, 2000 at 03:45:49PM +0100, M.-A. Lemburg wrote:
I had expected that in try: except: else the else clause always got executed, but it seems not for return
I think Robin mixed up try...finally with try...except...else. The finally clause is executed even in case an exception occurred.
(MAL and I already discussed this in private mail: Robin did mean try/except/else, and 'finally' already executes when returning directly from the 'try' block, even in Python 1.5)
This code: try: return except: pass else: print "returning" will indeed not print 'returning', but I believe it's by design. I'm against changing it, in any case, and not just because it'd break code :) If you want something that always executes, use a 'finally'. Or don't return from the 'try', but return in the 'else' clause. The 'except' clause is documented to execute if a matching exception occurs, and 'else' if no exception occurs. Maybe the intent of the 'else' clause would be clearer if it was documented to 'execute if the try: clause finishes without an exception being raised' ? The 'else' clause isn't executed when you 'break' or (after applying my continue-in-try patch ;) 'continue' out of the 'try', either. Robin... Did I already reply this, on python-list or to you directly ? I distinctly remember writing that post, but I'm not sure if it arrived. Maybe I didn't send it after all, or maybe something on mail.python.org is detaining it ? -- Thomas Wouters <thomas@xs4all.net> Hi! I'm a .signature virus! copy me into your .signature file to help me spread!

On Thu, 28 Dec 2000 21:21:15 +0100, Thomas Wouters <thomas@xs4all.net> wrote:
This can certainly be clarified in the documentation -- please file a bug report at http://sourceforge.net/projects/python/. Thanks! -Fred -- Fred L. Drake, Jr. <fdrake at acm.org> PythonLabs at Digital Creations

[Fred]
This can certainly be clarified in the documentation -- please file a bug report at http://sourceforge.net/projects/python/.
Here you go: https://sourceforge.net/bugs/?func=detailbug&bug_id=127098&group_id=5470

[Fred, suggested doc change near the end] [Thomas Wouters]
Guido's out of town again, so I'll channel him: Thomas is correct on all counts. In try/else, the "else" clause should execute if and only if control "falls off the end" of the "try" block. IOW, consider: try: arbitrary stuff x = 1 An "else" clause added to that "try" should execute when and only when the code as written executes the "x = 1" after the block. When "arbitrary stuff" == "return", control does not fall off the end, so "else" shouldn't trigger. Same thing if "arbitrary stuff" == "break" and we're inside a loop, or "continue" after Thomas's patch gets accepted.
The 'except' clause is documented to execute if a matching exception occurs, and 'else' if no exception occurs.
Yup, and that's imprecise: the same words are used to describe (part of) when 'finally' executes, but they weren't intended to be the same.
Sorry, I don't find that any clearer. Let's be explicit: The optional 'else' clause is executed when the 'try' clause terminates by any means other than an exception or executing a 'return', 'continue' or 'break' statement. Exceptions in the 'else' clause are not handled by the preceding 'except' clauses.
Hey, now you're channeling me <wink>! Be afraid -- be very afraid.
participants (8)
-
Fred L. Drake
-
M.-A. Lemburg
-
Mark Hammond
-
Michael Hudson
-
Moshe Zadka
-
Moshe Zadka
-
Thomas Wouters
-
Tim Peters