Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'finally:': 0.07; 'clause': 0.09; 'except:': 0.09; 'jessica': 0.09; 'reached.': 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; 'def': 0.12; 'jan': 0.12; 'wrote': 0.14; '"finally"': 0.16; '(true)': 0.16; 'clause.': 0.16; 'discussion.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'return,': 0.16; 'wrote:': 0.18; 'appears': 0.22; 'code,': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'mind.': 0.24; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply- To:1': 0.27; 'am,': 0.29; 'raise': 0.29; 'raised': 0.31; 'this.': 0.32; 'checking': 0.33; 'except': 0.35; 'something': 0.35; 'false': 0.36; 'keyword': 0.36; 'to:addr:python-list': 0.38; 'rather': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'dave': 0.60; 'most': 0.60; 'received:173': 0.61; "you're": 0.61; 'first': 0.61; "you'll": 0.62; 'happen': 0.63; 'finally': 0.65; 'finish': 0.65; 'received:fios.verizon.net': 0.84; 'returns.': 0.84; 'angel': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Try-except-finally paradox Date: Fri, 31 Jan 2014 00:26:05 -0500 References: <9314ac52-a2be-4382-94ef-2c291f32be1a@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-254-207.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 In-Reply-To: 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: 80 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1391145984 news.xs4all.nl 2837 [2001:888:2000:d::a6]:58333 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:65094 On 1/30/2014 7:05 AM, Dave Angel wrote: > Jessica Ross Wrote in message: >> I found something like this in a StackOverflow discussion. >>>>> def paradox(): >> ... try: >> ... raise Exception("Exception raised during try") >> ... except: >> ... print "Except after try" >> ... return True >> ... finally: >> ... print "Finally" >> ... return False >> ... return None >> ... >>>>> return_val = paradox() >> Except after try >> Finally >>>>> return_val >> False >> >> I understand most of this. >> What I don't understand is why this returns False rather than True. Does the finally short-circuit the return in the except block? >> > > The finally has to happen before any return inside the try or the > except. And once you're in the finally clause you'll finish it > before resuming the except clause. Since it has a return, that > will happen before the other returns. The one in the except block > will never get reached. > > It's the only reasonable behavior., to my mind. Checking with the disassembled code, it appears that the except return happens first and is then caught and the value over-written 2 0 SETUP_FINALLY 45 (to 48) 3 SETUP_EXCEPT 16 (to 22) 3 6 LOAD_GLOBAL 0 (Exception) 9 LOAD_CONST 1 ('Exception raised during try') 12 CALL_FUNCTION 1 (1 positional, 0 keyword pair) 15 RAISE_VARARGS 1 18 POP_BLOCK 19 JUMP_FORWARD 22 (to 44) 4 >> 22 POP_TOP 23 POP_TOP 24 POP_TOP 5 25 LOAD_GLOBAL 1 (print) 28 LOAD_CONST 2 ('Except after try') 31 CALL_FUNCTION 1 (1 positional, 0 keyword pair) 34 POP_TOP 6 35 LOAD_CONST 3 (True) 38 RETURN_VALUE 39 POP_EXCEPT 40 JUMP_FORWARD 1 (to 44) 43 END_FINALLY >> 44 POP_BLOCK 45 LOAD_CONST 0 (None) 8 >> 48 LOAD_GLOBAL 1 (print) 51 LOAD_CONST 4 ('Finally') 54 CALL_FUNCTION 1 (1 positional, 0 keyword pair) 57 POP_TOP 9 58 LOAD_CONST 5 (False) 61 RETURN_VALUE 62 END_FINALLY 10 63 LOAD_CONST 0 (None) 66 RETURN_VALUE -- Terry Jan Reedy