[英]If statement not working despite having triggered the appropriate boolean value | Python
I want to get this if statement to execute when good_view_list
is True
, I'm aware that it's a list but whenever I print out its boolean it gives me a True
value (I'm assuming because there are strings inside), why then doesn't this if good_view_time is True:
if statement work if good_view_time
is in fact True
.我想在
good_view_list
为True
时执行这个 if 语句,我知道它是一个列表,但是每当我打印出它的 boolean 它给我一个True
值(我假设因为里面有字符串),为什么不if good_view_time is True:
good_view_time
True
if 语句有效。
I'm aware of other alternatives, just want to know why THIS one doesn't work.我知道其他选择,只是想知道为什么这个不起作用。
good_view_time = ['https://www.instagram.com/p/CKmTcvmHYkY/', 'https://www.instagram.com/p/CKcOxtlHJsy/' , 'https://www.instagram.com/p/CKpHBAcHkhl/']
#returns True
print(bool(good_view_time))
if good_view_time is True:
for post in good_view_time:
print(post)
bool(good_view_time)
returns True
, but good_view_time
itself is a list, it can't be literally True
. bool(good_view_time)
返回True
,但good_view_time
本身是一个列表,它不能是字面上的True
。 good_view_time
is not True
, but converting it to Boolean gives True because non-empty lists are truthy. good_view_time
不是True
,但将其转换为 Boolean 会给出 True ,因为非空列表是真实的。 In the same vein, bool("Hello") is True
, but the string "Hello"
itself is most definitely not equal to True (why would it be? It's a string,), nor is it exactly equivalent to True
as the is
operator would check.同样,
bool("Hello") is True
,但字符串"Hello"
本身绝对不等于 True (为什么会这样?它是一个字符串,), is
完全等同于True
运营商会检查。
So, you should be comparing:所以,你应该比较:
if bool(good_view_time) is True:
Or, which is equivalent:或者,这是等效的:
if good_view_time:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.