any() and all() on empty list?

Steven D'Aprano steve at REMOVETHIScyber.com.au
Thu Mar 30 18:03:02 EST 2006


On Thu, 30 Mar 2006 11:28:54 -0800, Paul Rubin wrote:

> Steven D'Aprano <steve at REMOVEMEcyber.com.au> writes:
>> > No, all(seq) is true if you can't point to a specific element in seq
>> > that's false.
>> 
>> No, all(seq) is true if every element in seq is true. Surely that's a
>> more intuitive definition than your definition by what you can't do.
> 
> They are different?  

Of course they are different -- they differ in the case of an empty
sequence.

> I'd say every element in seq is true, unless
> there's an element that's false.  Do you want to pick a different word
> than "all" and suggest renaming the function?

I've already pointed out that I'm satisfied with Tim Peters' explanation
for why the defined behaviour for any() is *most often* the Right Way for
it to be implemented in Python, *but* there are circumstances that this
behaviour is incorrect, therefore the programmer needs to actually
consider carefully what should happen for the empty sequence case. Was I
not clear enough?

>> Here's another way of looking at the problem:
>> 
>> all(flying elephants which are pink) => true
>> all(flying elephants which are not pink) => true
>> 
>> So, these flying elephants -- are they pink or not?
> 
> By the definition, "all flying elephants are pink" and "all flying
> elephants are non-pink" are both true statements, if that's what
> you're asking.  There is no contradiction.

Of course there is a contradiction. The contradiction is that flying
elephants are simultaneously pink and not pink.

If you don't understand why "Foo is Bar" and "Foo is not Bar" can't both
be true simultaneously, I suggest you spend some time googling on
"noncontradiction logic". To get you started, here's the Wikipedia entry:

http://en.wikipedia.org/wiki/Law_of_noncontradiction


> It's one of those
> questions like "have you stopped beating your wife".

Think about it: what is the logical value of the boolean "I have stopped
beating my wife" in the case of somebody who never started beating
their wife?

if husband.stopped_beating_wife(): # returns True or False
    pay_fine()
else:
    go_to_jail()

Pretty hard on the innocent husbands who never even beat their wife at all.

What we're doing is running up to the limitations of Aristotelian
two-value logic. We're trying to pigeon-hole answers into True/False that
really don't fit, so of course there will be the occasional case where
neither True nor False is correct. In hacker culture, the Chinese word
"mu" (literally "without") is sometimes used to mean "I cannot answer that
question because your assumptions are not correct".

In the case of all(seq), the correct answer is "mu". But since we're
stuck with binary logic, the more commonly useful behaviour is True, but
sometimes that leads to problems, such as in my first example of Guido
being punished because he was found guilty of all the terrorist crimes he
committed -- which is an empty list.


-- 
Steven.




More information about the Python-list mailing list