为什么函数对象在python中评估为True? [英] Why do function objects evaluate to True in python?
问题描述
在python中进行如下构造是有效的:
In python it is valid to make a construction like:
def a():
return 0
if a:
print "Function object was considered True"
else:
print "Function object was considered False"
我想问一个函数指针被评估为True的逻辑是什么.
I wish to ask what is the logic that a function pointer is evaluated to True.
为什么在语言中插入了这种构造?
Why was this kind of construction inserted in the language?
推荐答案
许多事情在Python中的评价为True
.从关于布尔运算符的文档:
A lot of things evaluate to True
in Python. From the documentation on Boolean operators:
在布尔运算的上下文中,以及当控制流语句使用表达式时,以下值将解释为false:
False
,None
,所有类型的数字零以及空字符串和容器(包括字符串,元组,列表,字典,集合和Frozensets).所有其他值都解释为true.
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.
Functions in Python, like so many things, are objects, and not empty. Thus, in a boolean context, they evaluate to True.
这篇关于为什么函数对象在python中评估为True?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!