【发布时间】:2015-04-21 10:19:34
【问题描述】:
谁能解释这段代码发生了什么?
def some_function(value):
my_list = [value, [True, "Some text"]]
if True in my_list:
return "yes"
else:
return "no"
>>> print(function(1))
yes
>>> print(function("1"))
no
如果 value = 1,我得到“是”,这种行为是正确的。但如果值是某个字符串,则结果为“否”。我认为这应该始终是“是”。我用的是 Python3.4
【问题讨论】:
-
@Tichodroma: 标题编辑错误 - 问题说 ""value = 1, I got "yes" and this behavior is correct",暗示问题的问题是不是为什么
1 == True -
@Eric。同意。但是现在的标题也是错误的。该列表与问题无关。
-
True不在my_list中,但在my_list[1]中。