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


Groups > comp.lang.python > #105012 > unrolled thread

Re: empty clause of for loops

Started by"Sven R. Kunze" <srkunze@mail.de>
First post2016-03-16 13:16 +0100
Last post2016-03-16 13:16 +0100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: empty clause of for loops "Sven R. Kunze" <srkunze@mail.de> - 2016-03-16 13:16 +0100

#105012 — Re: empty clause of for loops

From"Sven R. Kunze" <srkunze@mail.de>
Date2016-03-16 13:16 +0100
SubjectRe: empty clause of for loops
Message-ID<mailman.198.1458130626.12893.python-list@python.org>
On 16.03.2016 11:47, Peter Otten wrote:
>
> What would you expect?

A keyword filling the missing functionality? Some Python magic, I 
haven't seen before. ;-)

>
>>>> class Empty(Exception): pass
> ...
>>>> def check_empty(items):
> ...     items = iter(items)
> ...     try:
> ...         yield next(items)
> ...     except StopIteration:
> ...         raise Empty
> ...     yield from items
> ...
>>>> try:
> ...    for item in check_empty("abc"): print(item)
> ... except Empty: print("oops")
> ...
> a
> b
> c
>>>> try:
> ...    for item in check_empty(""): print(item)
> ... except Empty: print("oops")
> ...
> oops

He will be highly delighted so see such a simplistic solution. ;-)

> I'm kidding, of course. Keep it simple and use a flag like you would in any
> other language:
>
> empty = True:
> for item in items:
>      empty = False
>      ...
> if empty:
>      ...
>

He likes this approach. Thanks. :-)


Although, I for one would like a keyword. I remember having this issue 
myself, and found that the "empty" variable approach is more like a 
pattern. As usual, patterns are workarounds for features that a language 
misses.

Best,
Sven

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web