Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #105012
| From | "Sven R. Kunze" <srkunze@mail.de> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: empty clause of for loops |
| Date | 2016-03-16 13:16 +0100 |
| Message-ID | <mailman.198.1458130626.12893.python-list@python.org> (permalink) |
| References | <56E93413.6090108@mail.de> <ncbdk4$h9u$1@ger.gmane.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
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: empty clause of for loops "Sven R. Kunze" <srkunze@mail.de> - 2016-03-16 13:16 +0100
csiph-web