empty clause of for loops
Sven R. Kunze
srkunze at mail.de
Wed Mar 16 10:29:46 EDT 2016
On 16.03.2016 13:57, Peter Otten wrote:
>
> I'd put that the other way round: syntactical support for every pattern
> would make for a rather unwieldy language. You have to choose carefully, and
> this requirement could easily be fulfilled by a function, first in your
> personal toolbox, then in a public libary, then in the stdlib.
>
> If you don't like exceptions implement (or find) something like
>
> items = peek(items)
> if items.has_more():
> # at least one item
> for item in items:
> ...
> else:
> # empty
>
> Only if such a function is used a lot or cannot be conceived without severe
> shortcumings adding to the syntax should be considered. The (hypothetical)
> question you should answer: which current feature would you throw out to
> make room for your cool new addition?
I am glad you asked. ;-)
I would re-use the "for-else" for this. Everything I thought I could
make use of the "-else" clause, I was disappointed I couldn't.
I find the addition to for-loop as useful as we already have a quite
complex try-except-else-finally clause. I don't know why for-loops
couldn't benefit from this as well.
Best,
Sven
More information about the Python-list
mailing list