在 python 中,这样的构造是有效的:
def a():
return 0
if a:
print "Function object was considered True"
else:
print "Function object was considered False"
我想问一下函数指针求值为True的逻辑是什么。
为什么要在语言中插入这种结构?
请您参考如下方法:
在 Python 中,很多事情的计算结果都是 True。来自documentation on Boolean operators :
In the context of Boolean operations, and also when expressions are used by control flow statements, the following values are interpreted as false:
False,None, numeric zero of all types, and empty strings and containers (including strings, tuples, lists, dictionaries, sets and frozensets). All other values are interpreted as true.
Python 中的函数,就像很多东西一样,是对象,而不是空的。因此,在 bool 上下文中,它们的计算结果为 True。
