bool evaluations of generators vs lists

Chris Rebert clp2 at rebertia.com
Tue Feb 10 17:05:19 EST 2009


On Tue, Feb 10, 2009 at 1:57 PM, Steven D'Aprano
<steven at remove.this.cybersource.com.au> wrote:
> On Tue, 10 Feb 2009 12:50:02 -0800, Josh Dukes wrote:
>
>> The thing I don't understand is why a generator that has no iterable
>> values is different from an empty list.
>
> How do you know it has no iterable values until you call next() on it and
> get StopIteration?
>
> By the way, your "has_values" function is just a slower version of the
> built-in any().

<nitpick>
Not quite: if the generator produces one or more elements but those
elements happen to be boolean false according to Python, then any()
will be false but has_values() will be true. The functions serve
different purposes (produces at least 1 value vs. has at least one
true value).

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com



More information about the Python-list mailing list