Path: csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'finally:': 0.05; 'badly': 0.07; 'creighton': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'wrote:': 0.16; 'first.': 0.18; 'laura': 0.18; 'try:': 0.18; '2015': 0.20; 'work,': 0.21; 'so.': 0.22; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'fri,': 0.27; 'skip:d 20': 0.34; 'done': 0.35; 'false': 0.35; 'but': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'missing': 0.37; 'wrong': 0.38; 'to:addr:python.org': 0.40; 'hope': 0.61; 'skip:n 10': 0.62; 'you.': 0.64; 'jul': 0.72; 'becker': 0.84; 'received:139': 0.91; 'try.': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Neal Becker Subject: Re: register cleanup handler Date: Fri, 24 Jul 2015 12:01:58 -0400 References: <201507241547.t6OFlh5t027843@fido.openend.se> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: exa2-in-fw-01-epn.hns.com User-Agent: KNode/4.14.9 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: , Newsgroups: comp.lang.python Message-ID: Lines: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1437753734 news.xs4all.nl 2906 [2001:888:2000:d::a6]:33839 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:94511 Laura Creighton wrote: > In a message of Fri, 24 Jul 2015 10:57:30 -0400, Neal Becker writes: >>I know we have try/finally, but I don't think that helps here, because >>code_executed_unconditionally couldn't be inside the try. Or am I missing >>something obvious? > > I think so. Either that or I am badly misunderstanding you. What is > wrong with > > try: > if (condition): > do_something_needing_cleanup > else: > do_something_else > code_executed_unconditionally > finally: > do_cleanup > > Laura do_cleanup has do be done only if do_something_needing_cleanup was done first. This would work, but is not very elegant. I hope for a better way. need_cleanup = False try: if (condition): do_something_needing_cleanup need_cleanup = True else: do_something_else code_executed_unconditionally finally: if need_cleanup: do_cleanup