Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ben Finney Newsgroups: comp.lang.python Subject: Re: Why is break allowed in finally, but continue is not? Date: Mon, 14 Dec 2015 09:27:50 +1100 Lines: 71 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de ftcT6iOhhLAYZMcNZ5XDuAcKiukbQQl7H+CjLB1HSgOA== Cancel-Lock: sha1:05CkTPGUUjLUdB1f/i25r5Vp8vs= Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'finally:': 0.05; 'clause': 0.07; 'hettinger': 0.07; 'agree,': 0.09; 'exception,': 0.09; 'raised,': 0.09; 'raised.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:Why': 0.09; 'python': 0.10; 'subject:not': 0.11; 'exception': 0.13; 'syntax': 0.13; "'break'": 0.16; '*any*': 0.16; 'clause.': 0.16; 'exception?': 0.16; 'exceptions.': 0.16; 'inclined': 0.16; 'opposite': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'runtimeerror': 0.16; 'sense,': 0.16; 'subject:break': 0.16; 'subject:continue': 0.16; 'swallow': 0.16; 'syntaxerror:': 0.16; 'those,': 0.16; 'odd': 0.18; 'try:': 0.18; ';-)': 0.18; 'prevent': 0.20; 'do.': 0.22; 'exceptions': 0.22; 'seems': 0.23; 'wrote': 0.23; 'this:': 0.23; 'tried': 0.24; 'testing': 0.25; 'header:User- Agent:1': 0.26; "doesn't": 0.26; 'example': 0.26; 'header:X -Complaints-To:1': 0.26; 'figure': 0.27; 'supported': 0.27; 'error': 0.27; 'idea': 0.28; 'prints': 0.29; 'raise': 0.29; 'print': 0.30; 'code': 0.30; 'raymond': 0.30; "i'd": 0.31; 'guess': 0.31; 'rules': 0.31; 'probably': 0.31; 'another': 0.32; "can't": 0.32; 'continuing': 0.32; 'statement': 0.32; 'run': 0.33; 'functions.': 0.35; "isn't": 0.35; 'but': 0.36; 'should': 0.36; 'instead': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'being': 0.37; 'received:org': 0.37; 'things': 0.38; 'difference': 0.38; 'why': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'your': 0.60; 'between': 0.65; 'finally': 0.70; 'skip:\xe2 10': 0.70; '8bit%:43': 0.72; 'treat': 0.72; '8bit%:46': 0.76; '_o__)': 0.84; 'batchelder': 0.84; 'direction:': 0.84; 'huh?': 0.84; 'received:125': 0.84; '\xe2\x80\x9cwe': 0.84; 'results,': 0.91; 'why?': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: jigong.madmonks.org X-Public-Key-ID: 0xAC128405 X-Public-Key-Fingerprint: 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 X-Public-Key-URL: http://www.benfinney.id.au/contact/bfinney-pubkey.asc X-Post-From: Ben Finney User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:100397 Ned Batchelder writes: > For testing coverage.py, I wrote a program to generate > randomly-structured Python functions. When compiling > the results, I got a message I'd never seen before: > > SyntaxError: 'continue' not supported inside 'finally' clause > > I guess this makes sense, when cleaning up from an > exception, continuing the loop seems an odd thing to do. > But 'break' is allowed in 'finally' clauses! I tried this: > > # Huh? This prints "here", and no exception is raised. > > for i in range(1): > try: > 1/0 > finally: > # If you change this to "continue", you get: > # 'continue' not supported inside 'finally' clause > break > print "here" > > The finally is perfectly willing to have a 'break', but it's > a syntax error to have 'continue'? Why? I don't see a > difference between the two when it comes to cleaning up > exceptions. Raymond Hettinger's answer is: The use of continue in a finally-clause is forbidden because its interpretation would have been problematic. […] The example he uses:: for i in range(10): print i try: raise RuntimeError finally: continue # if the loop continues, what would happen to the exception? print i What, in your opinion, should the above code do if instead of ‘continue’ some other flow-control statement is used? > There are other things you can do in a finally clause that > will prevent the exception from being raised, like 'return', > and 'break' works just fine. Hettinger doesn't defend those, and is dubious about ‘return’'s candidacy: Interestingly, you can put a return inside a finally-clause and it will swallow all exceptions including KeyboardInterrupt, SystemExit, and MemoryError. That probably isn't a good idea either ;-) > So why treat 'continue' specially? I am inclined to agree, but in the opposite direction: a case should be made for allowing *any* flow-control statement in an exception-handler's ‘finally’ clause. -- \ “We can't depend for the long run on distinguishing one | `\ bitstream from another in order to figure out which rules | _o__) apply.” —Eben Moglen, _Anarchism Triumphant_, 1999 | Ben Finney