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


Groups > comp.lang.python > #91824

Re: for...else

References <CANFcO8tOLorV1kuGJo6Vgb-e02EhC3NL2U7oiNRNF+b2s2M2ig@mail.gmail.com>
From Todd <toddrjen@gmail.com>
Date 2015-06-02 13:49 +0200
Subject Re: for...else
Newsgroups comp.lang.python
Message-ID <mailman.53.1433245778.13271.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

I think there is essentially zero chance of that.  My understanding is that
Guido regrets having "else" to begin with.

But this should work

broken = True
for x in it:
    if complicated_calculation_1():
        break
    complicated_calculation_2()
    if complicated_calculation_3():
        break
else:
    broken = False
if broken:
    cleanup()


On Tue, Jun 2, 2015 at 1:26 PM, acdr <mail.acdr@gmail.com> 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
>
> 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
>     ...
> --
> https://mail.python.org/mailman/listinfo/python-list
>

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


Thread

Re: for...else Todd <toddrjen@gmail.com> - 2015-06-02 13:49 +0200

csiph-web