[Python-Dev] Adding any() and all()

Raymond Hettinger python at rcn.com
Fri Mar 11 04:22:45 CET 2005

[Bill Janssen]
> I think I'd want them to be:
> def any(S):
>   for x in S:
>     if x:
>       return x
>   return S[-1]
> def all(S):
>   for x in S:
>     if not x:
>       return x
>   return S[-1]
> Or perhaps these should be called "first" and "last".


Over time, I've gotten feedback about these and other itertools recipes.
No one has objected to the True/False return values in those recipes or
in Guido's version.  

Guido's version matches the normal expectation of any/all being a
predicate.  Also, it avoids the kind of errors/confusion that people
currently experience with Python's unique implementation of "and" and

Returning the last element is not evil; it's just weird, unexpected, and
non-obvious.  Resist the urge to get tricky with this one.  

Raymond Hettinger

More information about the Python-Dev mailing list