any(), all() and empty iterable
Eduardo O. Padoan
eduardo.padoan at gmail.com
Sun Apr 12 13:59:10 EDT 2009
On Sun, Apr 12, 2009 at 8:53 AM, Tim Chase
<python.list at tim.thechases.com> wrote:
>>> From the docs:
>>
>> all(iterable)
>> Return True if all elements of the iterable are true.
>> Equivalent
>> to:
>> def all(iterable):
>> for element in iterable:
>> if not element:
>> return False
>> return True
>
> Then I'd say the comment is misleading. An empty list has no item that is
> true (or false), yet it returns true. The comment in the docs should read
> "Return False if any element of the iterable is not true" or "Return True if
> all elements of the iterable are true or if the iterable is empty."
I didn't knew about "Vacuous True" (my fault, it seems Discrete Math
101) until reading about on this thread (thanks everyone!), and
reading on wikipedia it answered this exact question.
> To get the behavior the original comment describes, would seem to require an
> implementation something like this:
>
> def all(iterable):
> iterable = iter(iterable)
> try:
> element = iterable.next()
> except StopIteration:
> raise UnderdefinedBecauseNoElementsToCompareToTrue
> while element:
> try:
> element = iterable.next()
> except StopIteration:
> return True
> return False
>
>
> Tweaking the documentation seems like an easier and more backwards
> compatible solution to me :)
>
> -tkc
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
Eduardo de Oliveira Padoan
http://importskynet.blogspot.com
http://djangopeople.net/edcrypt/
"Distrust those in whom the desire to punish is strong."
-- Goethe, Nietzsche, Dostoevsky
More information about the Python-list
mailing list