为什么函数对象在python中评估为True?
作者:互联网
在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中的函数就像很多东西一样,都是对象,而不是空的.因此,在布尔上下文中,它们的值为True.
标签:python,function,function-pointers 来源: https://codeday.me/bug/20191013/1904626.html