any(), all() and empty iterable

Tim Chase python.list at tim.thechases.com
Tue Apr 14 14:33:25 CEST 2009


Peter Pearson wrote:
> On Sun, 12 Apr 2009 06:53:24 -0500, Tim Chase 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."
> 
> How 'bout: "Return True if no element of the iterable is not true"?

I still prefer "Return False if any element of the iterable is 
not true" or "Return False if any element in the iterable is 
false" because that describes exactly what the algorithm does. 
Granted, anybody with a mote of Python skills can tell that from 
the algorithm, but if you're going to go to the trouble of 
documenting, you might as well document what it does.  The code 
in the docs do not check the truthiness of each element, it 
checks for the falseness (not-trueness) of each element.  One 
might be able to defend it using logical manipulations, but since 
Python strives for clarity...

-tkc






More information about the Python-list mailing list