[Python-ideas] Control Flow - Never Executed Loop Body
Vito De Tullio
vito.detullio at gmail.com
Sun Mar 20 19:39:24 EDT 2016
Robert Collins wrote:
>> Maybe I'm missing something, but shoulnd't be like
>>
>> if seq:
>> for elem in seq:
>> do_stuff(elem)
>> else:
>> do_empty_seq_stuff()
>>
>> a trivial code handling an empty list?
>
> seq = iter([])
>
> will cause your example to not run do_empty_seq_stuff().
oh, ok. sorry, didn't think about the iterator object.
...thinking about it, is there a reason why the boolean interpretation of an
empty iterator is true?
I know it's not trivial to know if an iterator is empty (still, there are a
pair of recipes online, like http://code.activestate.com/recipes/413614-testing-for-an-empty-iterator/ or the first part of the response at
http://stackoverflow.com/a/3114423/273593 ) but shoulnd't be more "pythonic"
to just have a "falsy" value for an "empty" iterator?
--
By ZeD
More information about the Python-ideas
mailing list