Hillel Wayne在《Logic for Programmers》中阐述了Python all() 函数的逻辑基础[1]。该著作通过数学论证解释了为何空列表上的 all() 函数返回 True——这源于 True 是逻辑与运算的恒等元[1]。根据这一性质,all(xs . ys) == all(xs) && all(ys) 对任意列表成立[1]。
同样的数学逻辑也适用于其他聚合操作[1]。空列表求和返回 0,而 any() 函数作用于空列表时返回 False,这些设计遵循同一套恒等元原则[1]。
Hillel Wayne explores the mathematical foundations of Python's all() function in "Logic for Programmers," demonstrating why an empty list returns True when passed to all() [1]. The explanation rests on a key principle: True serves as the identity element for logical AND operations, meaning that p && True == p for any value p [1]. This property ensures that the function maintains consistency across list concatenation, preserving the invariant that all(xs . ys) == all(xs) && all(ys) for any two lists [1].
Wayne uses this same logical framework to account for related behaviors in Python, including why an empty list sums to zero and why the any() function returns False on an empty list [1]. By grounding these seemingly arbitrary conventions in mathematical identity principles, the work illustrates how foundational algebra governs practical programming language design.