4

when using help(all), it returns:

all(iterable)=>bool
return True if bool(x) is True for all values x in the iterable.    
if the iterable is empty, return True

help(bool) returns:

bool(x) -> bool
 |  
 |  Returns True when the argument x is true, False otherwise.
 |  The builtins True and False are the only two instances of the class bool.
 |  The class bool is a subclass of the class int, and cannot be subclassed.

when trying:

>>>bool()
False

>>>all([])
True

my question is, in case that all's input is the empty list/dict/tuple(i.e. iterator), what's passed to bool?? and how come it returns True, as it's dependent on bool?

2
  • 5
    Universal quantification over the empty set is true. That's not a Python thing, that's just a fact. Commented Jun 26, 2014 at 4:53
  • 1
    Think of all as "no-False". An empty iterable contains no False-y values, so all returns True. Commented Jun 17, 2015 at 23:51

4 Answers 4

13

bool() is never invoked if all()'s argument is empty. That's why the docs point out the behavior of all() on an empty input as a special case.

The behavior bool() == False is irrelevant to what all() does in any case. By the way, in Python bool is a subclass of int, so bool() == False is necessary to be compatible with that int() == 0.

As to why, e.g., all([]) is True, it's to preserve useful identities. Most importantly, for any non-empty sequence x it's desirable that

all(x) == (bool(x[0]) and all(x[1:]))

and all([]) == True is the only result allowing that identity to hold for all values of x.

Sign up to request clarification or add additional context in comments.

Comments

7
all(iterable)

...is documented to be equivalent to;

def all(iterable):
    for element in iterable:
        if not element:
            return False
    return True

...which returns False if and only if any value in the list converts to False.

Since there is no element in the list you pass in, no element converts to False, and the result of the function call is True.

Comments

6

[…] what's passed to bool?

Nothing – and not the “nothing” of bool(). bool is never called. Is every element in an empty sequence truthy? Yes! There aren’t any that aren’t, after all.

5 Comments

While I agree with the logic, you could equally argue that all elements are false - the important thing is that it's documented behavior
@PeterGibson: You could say that all elements are false, and you would be right, but that doesn’t have any bearing on all, since it doesn’t check whether all elements are false and shouldn’t in the case of a non-empty list either. (That is to say, not all elements are false in [True, False], but all will still obviously return False.)
@PeterGibson: You could argue that, and you could even implement it that way in your own language, but it turns out that that convention is much less useful. Making all([]) == True results in the invariant that for any list l and element x, all(l + [x]) is True if and only if all(l) and bool(x) are True, even in the case of the empty list l. Setting all([]) == False breaks that invariant.
On further consideration, I realise you're both right. The point I (meant) to make is that sometimes the documented behavior may not agree with what seems logical, however it is correct because it is documented behavior.
@PeterGibson Sort of the other way around. It's documented that way because it's logical. As in, it's how mathematicians would do it. Yes, they're all true, and they're all false. But "all" only asks, "Are all of them true?" A hypothetical allnot ("Are all of them false?") would also return true in the empty case. See en.wikipedia.org/wiki/Vacuous_truth
2

My guess is that it's implemented something like:

def all(inp):
    return not any(not bool(x) for x in inp)

so an empty iterator is "vacuously true." This makes sense, and corresponds with similar notions in logic -- we usually say that a universal quantifier holds of some proposition over a domain even if an existential one doesn't.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.