any() and all() on empty list?

Steven D'Aprano steve at
Wed Mar 29 13:34:08 CEST 2006

At risk of flogging a dead horse...

On Wed, 29 Mar 2006 01:01:00 -0800, vdrab wrote:

>> I'm completely on board with the semantics for any().  But all() bothers
>> me.  If all() receives an empty list, it will return True, and I don't
>> like that.  To me, all() should be a more restrictive function than any(),
>> and it bothers me to see a case where any() returns False but all()
>> returns True.
> Who should we call to report this fallacy? GvR? Goedel? Tarski? no,
> wait... Frege ! or wait... actually, I think that must be Aristotle.
> Sorry Aristotle, the ol' syllogisms have to go.
> ; -)
> All silliness aside, the meaning of all() in python corresponds just
> fine with "all" in both language and logic.
> s.

(Dis)proof by counter-example:

GvR is in deep trouble -- the police now have sufficient evidence of his
guilt to lock him away for life for all of the terrorist attacks he is
suspected of:

>>> def enough_evidence(crime):
...     return True
>>> suspected_attacks = []
>>> sufficient_proof = filter(enough_evidence, suspected_attacks)
>>> guilty = all(sufficient_proof)
>>> if guilty:
...     print "Send him to Gitmo!"
Send him to Gitmo!

I understand and accept Tim Peter's explanation for why the proposed
behaviour is the Right Way to handle all() and any() -- but that doesn't
mean that there isn't a paradox buried in there.

Notice that the police, by setting themselves the more difficult task of
proving Guido's guilt on all() charges, can lock him up even though no
crime was committed. Whereas, if they took the simpler task of merely
proving his guilt on any() charge, Guido would be a free man:

>>> guilty = any(sufficient_proof)
>>> if not guilty:
...     print "Walk free!"
Walk free!

While the implemented behaviour might be more practical than the
alternatives, it is still worrying paradoxical. If "All sheep are woolly",
then obviously it must also be true that "Any sheep is woolly". More
formally, if all(X), then any(X) -- except for the case of empty X. Hmmm.


More information about the Python-list mailing list