Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!dedekind.zen.co.uk!zen.net.uk!hamilton.zen.co.uk!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail 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; 'value,': 0.04; 'finally:': 0.07; 'level,': 0.07; 'odd': 0.07; 'clause': 0.09; 'executed': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'try:': 0.09; 'wrote': 0.14; 'block.': 0.16; 'cleanup,': 0.16; 'docs.': 0.16; 'occurs,': 0.16; 'precedence.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'statement.': 0.16; 'variable.': 0.16; 'exception': 0.16; 'files.': 0.16; 'bit': 0.19; 'seems': 0.21; 'certainly': 0.24; 'regardless': 0.24; 'first,': 0.26; 'this:': 0.26; 'subject:/': 0.26; 'header:X-Complaints- To:1': 0.27; 'function': 0.29; 'except': 0.35; 'something': 0.35; 'really': 0.36; 'should': 0.36; 'to:addr:python-list': 0.38; 'received:71': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'even': 0.60; 'course': 0.61; 'finally': 0.65; 'frank': 0.68; 'subject:try': 0.84; 'subject::': 0.85; 'imagine': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dave Angel Subject: Re:try/except/finally Date: Sat, 7 Jun 2014 21:59:04 -0500 (CDT) Organization: news.gmane.org References: <0a89c96d-de62-42ad-be48-6107ce10d215@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-71-97-198-161.hstntx.dsl-w.verizon.net X-Newsreader: PiaoHong.NewsGroup.Client.VIP:1.53 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 48 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1402196293 news.xs4all.nl 2892 [2001:888:2000:d::a6]:54262 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:72950 Frank B Wrote in message: > Ok; this is a bit esoteric. > > So finally is executed regardless of whether an exception occurs, so states the docs. > > But, I thought, if I from my function first, that should take precedence. > > au contraire > > Turns out that if you do this: > > try: > failingthing() > except FailException: > return 0 > finally: > return 1 > > Then finally really is executed regardless... even though you told it to return. > > That seems odd to me. > The thing that's odd to me is that a return is permissible inside a finally block. That return should be at top level, even with the finally line. And of course something else should be in the body of the finally block. If you wanted the finally block to change the return value, it should do it via a variable. retval = 0 try: failingthing() except FailException: return retval finally: retval =1 return something I imagine the finally clause was designed to do cleanup, like closing files. And it certainly predated the with statement. -- DaveA