空列表等于无?

空列表等于无?

问题描述:

可能重复:
为什么"[] == False"评估为False什么时候如果不是[]"成功?

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!