Is this PEP-able? fwhile
Joshua Landau
joshua.landau.ws at gmail.com
Tue Jun 25 04:04:43 EDT 2013
On 25 June 2013 00:13, Tim Chase <python.list at 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.
More information about the Python-list
mailing list