Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #91827

Re: for...else

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'broken': 0.03; 'plenty': 0.07; '"if': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'def': 0.14; '"break"': 0.16; 'advantage.': 0.16; 'constructs': 0.16; 'flag,': 0.16; 'fond': 0.16; 'obviously,': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.16; 'expanded': 0.18; 'language': 0.19; 'trying': 0.22; 'of.': 0.22; 'code,': 0.23; 'wondering': 0.25; 'header:User-Agent:1': 0.26; 'header:X -Complaints-To:1': 0.26; 'equivalent': 0.27; 'expanding': 0.27; 'separate': 0.27; 'this.': 0.28; "i'm": 0.29; 'currently,': 0.29; 'feature,': 0.29; 'programmers': 0.31; "i'd": 0.31; 'code': 0.31; "can't": 0.32; 'gets': 0.32; 'done,': 0.33; 'add': 0.34; 'to:addr :python-list': 0.35; 'really': 0.35; 'but': 0.36; 'too': 0.36; 'there': 0.36; 'two': 0.37; 'hi,': 0.37; 'subject:: ': 0.37; 'front': 0.38; 'rather': 0.38; 'received:org': 0.38; 'someone': 0.38; 'to:addr:python.org': 0.39; 'where': 0.40; 'received:de': 0.40; 'called': 0.40; 'even': 0.61; 'more': 0.62; 'places': 0.64; 'of:': 0.66; 'apart': 0.70; 'obvious': 0.72; 'demand': 0.79
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: for...else
Date Tue, 02 Jun 2015 14:27:35 +0200
Organization None
References <CANFcO8tOLorV1kuGJo6Vgb-e02EhC3NL2U7oiNRNF+b2s2M2ig@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p57bd989b.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.54.1433248099.13271.python-list@python.org> (permalink)
Lines 70
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1433248099 news.xs4all.nl 2883 [2001:888:2000:d::a6]:52778
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:91827

Show key headers only | View raw


acdr wrote:

> 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

Perhaps you like of these:

for x in it:
    if not complicated_calculation_1():
        complicated_calculation_2()
        if not complicated_calculation_3():
            continue
    cleanup()
    break

def f():
    for x in it:
        if complicated_calculation_1():
            break
        complicated_calculation_2()
        if complicated_calculation_3():
            break
    else:
        return
    cleanup()

> 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
>     ...

You may not like using a flag, but it's a really obvious approach and the 
situations where it's necessary are not very common. Apart from indentation-
based blocks Python is very middle-of-the-road, so I'd ask if there is an 
existing language that has such a feature, and if yes, is it used 
frequently? I suspect that there are many Python programmers that have never 
even used (for...else).

Python the language is already becoming too complicated for my taste; I 
would rather not add more constructs where there is no significant 
advantage. 

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: for...else Peter Otten <__peter__@web.de> - 2015-06-02 14:27 +0200

csiph-web