我知道它有一个很好的理由,但我想知道是什么原因?
>>> print all([])
True
如果 all() 旨在检查 iterable 上的每个项目是否评估为“True”,并且我们知道空列表被评估为 False
>>> bool([])
False
那么为什么 all() 为空列表返回 True 呢?
<编辑>
我已经阅读了文档,并且我知道实现
def all(iterable):
for element in iterable:
if not element:
return False
return True
但问题是为什么不呢?
def all(iterable):
if not iterable:
return False
for element in iterable:
if not element:
return False
return True
这有逻辑吗?如果您有已完成任务的列表
today_todo_status = [task.status for task in my_todo if task.date == today]
can_i_go_home = all(today_todo_status)
好的,在上面的假设示例中,如果我没有任务,那么我就可以回家了。
但是还有其他情况,我不认为 all() 是为待办事项列表制作的。哈哈
</编辑>