[Python-ideas] for/else syntax

Yuvgoog Greenle ubershmekel at gmail.com
Sat Oct 3 19:05:53 CEST 2009


You do have a point and I'll meditate on it. But I do think the
situation is better than you made it appear.

There will usually be a linebreak. We can't help the readability of
code written by people that compress code.

So a more fair representation would be:

if not condition:
    break
if not break:
    condition

(note: I'm not sure what you meant by "if not break: condition")

The bad case is when you have nested loops so it would look like this...

for item in list_of_items:
    while condition:
        if reason_to_stop():
            break
    if break:
        # do something after the while broke
    if other_condition:
        break

On Sat, Oct 3, 2009 at 7:51 PM, Ron Adam <rrr at ronadam.com> wrote:
>
>
> Yuvgoog Greenle wrote:
>>
>> On Sat, Oct 3, 2009 at 7:19 PM, Ron Adam <rrr at ronadam.com> wrote:
>>>
>>> True, and that is part of the other 20% I expect the documentation
>>> would't
>>> help.
>>>
>>> The only solution to that that I can think of is to change the esle's in
>>> for
>>> and while loops to for/then and while/then in Python 5.0.
>>>
>>> It might be doable, and if enough people want it, then just maybe...
>>>
>>
>> Documenting this syntax with the appropriate warnings and clear
>> explanations is very important.
>>
>> But I don't think the amount of code mishaps on this thread are
>> properly addressed just by better documentation. The syntax I proposed
>> is harmless, it won't break any existing code, and it's something
>> people in python 3 can use right now. Deprecation etc can wait for
>> python 4. Don't tea5e me :)
>
> Lol, Sorry about that. ;-),  but I do think it's too late to change for/else
> and while/else to for/then and while/then in python 4.0 already.
>
>
>> Of course people here would have to say if my idea sounds good or not.
>>
>> while still_more_items():
>>    if found_it():
>>        break
>> if not break:
>>    # exhausted search space
>>
>
> I suppose most people won't like the way break is used in two entirely
> different ways depending on where it is.
>
> When you see a break, two things become really clear:
>
>   1: you are inside a loop
>
>   2: we are exiting a loop
>
>
> When you use it in the other context it makes those things a bit less
> obvious especially if you consider loops inside of loops.  We then have to
> consider the closeness these too statements are to each other.
>
>   if not condition: break
>
>   if not break: condition
>
> we aren't exiting a loop in the second case, even though it may be inside
> another loop.
>
>
> Cheers,
>   Ron
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



More information about the Python-ideas mailing list