[Python-ideas] for/except/else syntax
ilya
ilya.nikokoshev at gmail.com
Wed Oct 7 21:18:59 CEST 2009
I was actually surprise to learn that for/else is already part of
Python. I would be more happy with 'if not break', but anyway.
As for `loop.empty`, this is probably what I would reserve if I was
designing language from scratch, but it does seem very unpythonic to
me. Note though that you can do
empty = object() # sentinel
...
i = empty
for i in SEQ:
A
if i is empty:
B # suite_if_loop_body_is_not_executed
This requires just one more line (i=empty) per loop compared to other
proposals and is completely unambiguos to both human and computer.
Moreover, you can put `empty` into builtins without breaking existing
programs (however, this idiom breaks in a weird case of
`SEQ[-1]==empty`).
On Wed, Oct 7, 2009 at 11:00 AM, Yuvgoog Greenle <ubershmekel at gmail.com> wrote:
> I don't like your "except" and "else" only because it's not obvious
> when they execute.
>
> btw your example breaks the current "for..else" search idiom. C,
> suite_if_loop_is_not_broken, should be labelled
> suite_if_loop_is_not_broken_and_not_empty in your example code.
>
> As I understand it, pythoneers need a way to know what happened with a
> previous loop. Did it break? Did it execute at all? The answer to both
> questions is either True or False and developers need these answers
> only after the loop finished. If python wasn't in over it's head with
> reserved words I would suggest adding "loop":
>
> for i in SEQ:
> A
> if not loop.broke:
> C # suite_if_loop_is_not_broken
> elif loop.empty:
> B # suite_if_loop_body_is_not_executed
>
> For optimization I would suggest the "loop" variable only be
> instantiated if it's referenced. Also, there may be better names for
> "loop", "broke" and "empty".
>
> Two drawbacks:
> 1. python's in over it's head with reserved words.
> 2. developers might want to use the "loop" variable a mile down from
> the loop which would be ugly but their fault.
>
> I'm writing a summary of the previous for/else thread and hope that by
> the time I finish that (a week+), someone will invent a readable,
> low-cost syntax (optional but awesome if it can handle all 4:
> break/not break/empty/not empty).
>
> On Wed, Oct 7, 2009 at 6:06 AM, Bruce Leban <bruce at leapyear.org> wrote:
>> Here you go:
>> do_except = True
>> for i in SEQ:
>> do_except = False
>> A
>> else:
>> if not do_except:
>> C # suite_if_loop_is_not_broken
>> if do_except:
>> B # suite_if_loop_body_is_not_executed
>> --- Bruce
>>
>>
>> On Tue, Oct 6, 2009 at 8:53 PM, Guido van Rossum <guido at python.org> wrote:
>>>
>>> On Tue, Oct 6, 2009 at 8:46 PM, Bruce Leban <bruce at leapyear.org> wrote:
>>> > Switching gears a bit, here's what I want:
>>> >
>>> > for_stmt ::= "for" target_list "in" expression_list ":" suite_loop_body
>>> > ["except" ":" suite_if_loop_body_is_not_executed] ["else" ":"
>>> > suite_if_loop_is_not_broken]
>>> >
>>> > The except and else appear in this order to make it clear that except
>>> > takes
>>> > precedence. If the loop body is not executed at all then both conditions
>>> > are
>>> > true but it obviously is only useful if except takes precedence.
>>> > Comments?
>>>
>>> I'm sure it's obvious to you what the except clause should do, but it
>>> isn't to me. :-( Assuming you're not proposing we change the meaning
>>> of else in the absence of except, and that its meaning together with
>>> except is a logical extension of this, can you show how we should
>>> translate a for-loop with an except clause into current Python? Say,
>>> what would I be writing today to get the exact effect of your proposed
>>>
>>> for i in SEQ:
>>> A
>>> except:
>>> B
>>> else:
>>> C
>>>
>>> ?
>>>
>>> --
>>> --Guido van Rossum (home page: http://www.python.org/~guido/)
>>
>>
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> http://mail.python.org/mailman/listinfo/python-ideas
>>
>>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
More information about the Python-ideas
mailing list