Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!border1.nntp.ams1.giganews.com!nntp.giganews.com!bcyclone02.am1.xlned.com!bcyclone02.am1.xlned.com!newsfeed.xs4all.nl!newsfeed4a.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.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'else:': 0.03; 'broken': 0.03; 'plenty': 0.07; '"if': 0.09; '"break"': 0.16; 'fond': 0.16; 'obviously,': 0.16; 'expanded': 0.18; 'trying': 0.22; 'of.': 0.22; 'code,': 0.23; 'wondering': 0.25; 'equivalent': 0.27; 'expanding': 0.27; 'separate': 0.27; 'message-id:@mail.gmail.com': 0.28; 'this.': 0.28; "i'm": 0.29; 'currently,': 0.29; 'code': 0.31; "can't": 0.32; 'gets': 0.32; 'done,': 0.33; 'received:google.com': 0.34; 'to:addr:python-list': 0.35; 'really': 0.35; 'there': 0.36; 'two': 0.37; 'hi,': 0.37; 'front': 0.38; 'someone': 0.38; 'to:addr:python.org': 0.39; 'where': 0.40; 'called': 0.40; 'places': 0.64; 'of:': 0.66; 'demand': 0.79 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=cF4PqITAvLd89qVZeUEv/9i5S4pJ3GUxsnbEC/atMN8=; b=lg8v7lducHfa/95zIjFvX60SIeXCVj5z6GuVrj69qnQ8LkPu9Z7RMazv5wT5YtWkfx RLViiCnTW+X7+8FNVKMrMRd8f8YtZm5H6p+F1vzkwjx1ubTl18RhbDcGP5GuAoUdDmB5 RbJESN3DtwIPD/bpgbvsyit5NHkCztzJvHboBEygwCMwbvfSMM7zDdqeJdc+JtvD5Z2V RsPl7SAmQGL7y69UObXnsAL8m0AzmkDFbNO/SiAJ2twDZS5kJjTmZ4I3XxIATim16L4I toeKIjactKuCGGbAK0XFah57TZ56lWKgKQEOAN9g2QkTBFtxTp0JmBDl4s3o9U73g+Bm 52bA== MIME-Version: 1.0 X-Received: by 10.194.176.225 with SMTP id cl1mr49214322wjc.45.1433244402691; Tue, 02 Jun 2015 04:26:42 -0700 (PDT) Date: Tue, 2 Jun 2015 13:26:42 +0200 Subject: for...else From: acdr To: python-list@python.org Content-Type: text/plain; charset=UTF-8 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: 34 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1433244404 news.xs4all.nl 2881 [2001:888:2000:d::a6]:34494 X-Complaints-To: abuse@xs4all.nl X-Received-Bytes: 3840 X-Received-Body-CRC: 3774384177 Xref: csiph.com comp.lang.python:91821 Hi, Currently, in various places in my code, I have the equivalent of: for x in it: if complicated_calculation_1(): cleanup() break complicated_calculation_2() if complicated_calculation_3(): cleanup() break Obviously, I'm repeating myself by having two separate calls to cleanup(). I can't really see a nicer way to do this. (Though I see plenty of non-nice ways to do this, such as adding "broken = True" in front of every "break", and then after the loop is done, have an "if broken" section.) Other solutions that I'm not particularly fond of can be found on stackexchange, where someone else is trying to do the same thing: http://stackoverflow.com/questions/3296044/opposite-of-python-for-else I'm wondering if there is a demand for expanding the "for...else" functionality to be expanded also have a block of code that only gets called if the loop is broken out of. I.e.: for x in it: ... then: # "break" was called ... else: # "break was not called ...