我搜索了关于 all
的理解在 Python 中的函数,我找到了 this ,根据这里:
all
will returnTrue
only when all the elements are Truthy.
但是当我使用这个函数时,它的行为有所不同:
'?' == True # False
'!' == True # False
all(['?','!']) # True
为什么当输入中的所有元素都是 False
时它返回 True
?是我误解了它的功能还是有解释?
最佳答案
only when all the elements are Truthy.
Truthy != True
。
all
本质上检查 bool(something)
是否为 True
(对于可迭代对象中的所有 something
) .
>>> "?" == True
False
>>> "?" == False # it's not False either
False
>>> bool("?")
True
关于python - Python 中的 "all"函数是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35685768/