[Python-ideas] Break the dominance of boolean values in boolean context
steve at pearwood.info
Thu Sep 15 00:44:13 CEST 2011
> What should any() return?
I'm not sure I understand the question, because any() already has an
answer, and changing it would be a backwards-incompatible change. So I
don't quite understand the point of the question: the answer is "exactly
what it already returns".
Likewise for all:
This is known as "vacuous truth":
As far as justifying the results returned by any() and all(), the
decision to return False and True respectively seems to work well for
me. I have rarely, if ever, needed the opposite behaviour:
if not mylist or any(mylist):
if mylist and all(mylist):
so, at least for me, the behaviour of all() and any() seems like the
However, I point out that if the argument is an iterator instead of a
sequence, it's significantly harder:
flag = bool(next(it))
flag = False # instead of True
flag = all(it)
Perhaps all() and any() should take an optional argument to return if
the iterable is empty?
More information about the Python-ideas