and and or return the last element they evaluated, but why doesn’t Python’s built-in function any?
I mean it’s pretty easy to implement oneself like this, but I’m still left wondering why.
def any(l):
for x in l:
if x:
return x
return x
edit:
To add to the answers below, here’s an actual quote from that same mailing list of ye mighty emperor on the issue:
Whether to always return True and False or the first faling / passing
element? I played with that too before blogging, and realized that the
end case (if the sequence is empty or if all elements fail the test)
can never be made to work satisfactory: picking None feels weird if
the argument is an iterable of bools, and picking False feels weird if
the argument is an iterable of non-bool objects.Guido van Rossum (home page: http://www.python.org/~guido/)
This very issue came up up on the Python developer’s mailing list in 2005, when Guido Van Rossum proposed adding
anyandallto Python 2.5.Bill Janssen requested that they be implemented as
Raymond Hettinger, who implemented
anyandall, responded specifically addressing whyanyandalldon’t act likeandandor:The mailing list largely concurred, leaving the implementation as you see it today.