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


Groups > comp.lang.python > #49140

Re: Is this PEP-able? fwhile

References <8D03F2B8CF0E7BE-1864-1796B@webmail-m103.sysops.aol.com> <CAPTjJmrwNyF2-J8F3ii8W4WpkrNC67+i88ODLyiqHGa_7bz0Xg@mail.gmail.com> <20130624173050.5e16debf@bigbox.christie.dr> <CAA=1kxRp6o4OX3ES70j+O0xrSPfyyoZsU5x-qvNegCaY3QBYaw@mail.gmail.com> <20130624181323.642ee428@bigbox.christie.dr>
From Joshua Landau <joshua.landau.ws@gmail.com>
Date 2013-06-25 09:04 +0100
Subject Re: Is this PEP-able? fwhile
Newsgroups comp.lang.python
Message-ID <mailman.3803.1372147834.3114.python-list@python.org> (permalink)

Show all headers | View raw


On 25 June 2013 00:13, Tim Chase <python.list@tim.thechases.com> wrote:
> On 2013-06-24 23:39, Fábio Santos wrote:
>> On 24 Jun 2013 23:35, "Tim Chase" wrote:
>> > On 2013-06-25 07:38, Chris Angelico wrote:
>> > > Python has no issues with breaking out of loops, and even has
>> > > syntax specifically to complement it (the 'else:' clause). Use
>> > > break/continue when appropriate.
>> >
>> > from minor_gripes import breaking_out_of_nested_loops_to_top_level
>>
>> for x, y in itertools.product(range(width), range(height)):
>
> This works nicely for certain use cases, but if there's additional
> processing that needs to be done in the outer loops, it starts to get
> hairy.  As Ian Kelly mentions, I could really dig a labeled
> break/continue in Python (it's one of the few ideas I like that Java
> made pretty popular; though I can't say I particularly care for
> Java's implementation).  I'd love to see something like a decorator
> where you could do things like the following pseudocode:
>
>   @toplevel
>   for i in range(height):
>     for j in range(width):
>       for component in data[i,j]:
>         if condition:
>           continue toplevel
>         elif other_condition:
>           break toplevel
>         else:
>           other processing
>
> I'm not sure such a feature would ever arrive, but it would make it
> easier than the current recommendation which is usually to either (1)
> make inner loops into functions from which you can return; or (2)
> raise a custom exception and then catch it in the outer loop.

Guido says no; supposedly you can always just use a function. I don't
agree, but I'm not Guido.

Anyhoo, I prefer:

for i in range(height) as toplevel:
    for j in range(width):
        break from toplevel

Which reads like pure Python gold.

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


Thread

Re: Is this PEP-able? fwhile Joshua Landau <joshua.landau.ws@gmail.com> - 2013-06-25 09:04 +0100

csiph-web