r/learnpython • u/Lance_2564_ • Oct 19 '26
Huh? How does this work?
a = "hi"
if a:
print("Bye")
print(a == True)
print(a is True)The output is:
Bye
False
False
But if it's false, how come it printed "bye"?????? I don't get it.
44
Upvotes
View all comments
Show parent comment
2
u/gdchinacat Oct 20 '26
This has nothing to do with "non-zero". Python objects have a notion of 'truthiness' and is evaluated by calling __bool__() on it (which is implemented by object and can be overridden). While True and False are subclasses of int and have integer values (1 and 0 respectively), if evaluates truthiness (as determined by __bool__, not whether the "variable has a non-zero value".