Can anybody explain why in python builtin buinction all return True in this case all([])?
In [33]: all([])
Out[33]: True
In [34]: all([0])
Out[34]: False
In [35]: __builtins__.all([])
Out[35]: True
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I’m not convinced that any of the other answers have really address the question of why this should be the case.
The definition for Python’s
all()comes from boolean logic. If for example we say that “all swans are white” then a single black swan disproves the statement. However, if we say that “all unicorns are pink” logicians would take that as a true statement simply because there are no non-pink unicorns. Or in other words “all ” is vacuously true.Practically it gives us a useful invariant. If
all(A)andall(B)are both true then the combination ofall(A + B)is also true. Ifall({})was false we should have a less useful situation because combining two expressions one of which is false suddenly gives an unexpected true result.So Python takes
all([]) == Truefrom boolean logic, and for consistency with other languages with a similar construct.Taking that back into Python, in many cases the vacuous truth makes algorithms simpler. For example, if we have a tree and want to validate all of the nodes we might say a node is valid if it meets some conditions and all of its children are valid. With the alternative definition of
all()this becomes more complex as we have to say it is valid if it meets the conditions and either has no children or all its children are valid.