Why Python built in “all” function returns True for empty iterables?

后端 未结 7 829
萌比男神i
萌比男神i 2020-12-05 02:08

I know it has a good reason, but I want to know what reason?

>>> print all([])
True

If all() is intended to check if every item on

相关标签:
7条回答
  • 2020-12-05 03:05

    Suppose all([]) is False.

    Then, for all non empty list A, all(A + []) should be also False as

    all(A + []) = all(A) and all([])
                = all(A) and False
                = False
    

    Since A + [] = A, we know

    all(A + []) = all(A) for any non empty list A
    

    But, all(A) could be True (e.g., A = [True])

    Hence,

    for all non empty list A, all(A + []) should be also False

    This contradicts. As a result, the first assumption is wrong and

    all([]) is True

    0 讨论(0)
提交回复
热议问题