空列表等于无?
问题描述:
Possible Duplicate:
Why does "[] == False" evaluate to False when "if not []" succeeds?
根据python的三元运算符,我是python的新手
I am new to python as per ternary operator of python
>>> 'true' if True else 'false' true
true
我期望下面的代码输出为[],因为[]不等于None
i am expecting for below code output as [] because [] not equal to None
>>> a=[]
>>> a==None
False
>>> a if a else None
None
如果我错了,请纠正
谢谢 血气
答
空白列表[]
不等于None
.
但是,它可以求值为False
-也就是说,其真实性"值为False
. (请参阅OP上的注释中的源.)
However, it can evaluate to False
--that is to say, its "truthiness" value is False
. (See the sources in the comments left on the OP.)
因此,
>>> [] == False
False
>>> if []:
... print "true!"
... else:
... print "false!"
false!