any(), all() and empty iterable
research at johnohagan.com
Tue Apr 14 19:54:14 CEST 2009
On Tue, 14 Apr 2009, Carl Banks wrote:
> On Apr 12, 10:45 am, Tim Chase <python.l... at tim.thechases.com> wrote:
> > > That's why you ask "Do you have any books called 'Robinson Crusoe'?"
> > > rather than "Are all your books called 'Robinson Crusoe'?".
> > Mu. If I don't have any books..."Have you stopped beating all
> > your wives?" The question makes the presumption that I have
> > books (or that you've been beating your wife).
> A previous time we had this dicussion, I pointed out that natural
> language comparisons are questionable since in natural langauge you
> are not confined to two answers. The valid answer to the above
> question could be "I don't have any books", neither yes nor no. The
> closest thing to that you can get in Python is to raise an exception.
Agreed; having absorbed the answers to my original question, I now understand
why all('Robinson Crusoe' in books for books in ) - or with any object in
place of the string, or indeed just all() - doesn't return False, but,
vacuous truth notwithstanding (and it's apparently a not uncontroversial
notion), it doesn't seem fair to return True without so much as a
An exception, or at least a specific mention of the case of empty iterables in
the docs (as Tim Chase suggested), would have baffled me less than my program
telling me that the same items were both ubiquitous and entirely absent from
a list of sets!
Of course, it's possible people out there depend on the present behaviour.
More information about the Python-list