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
.
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
. good_view_time
is not True
, but converting it to Boolean gives True because non-empty lists are truthy. 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.
So, you should be comparing:
if bool(good_view_time) is True:
Or, which is equivalent:
if good_view_time:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.