I want to check if a variable is True
(not just its truthiness).
This is easily checked like this:
>>> a = 1
>>> isinstance(a, bool) and a
False
>>> a = True
>>> isinstance(a, bool) and a
True
Of course, that is totally fine. However, the same behaviour can be seen in the following code:
>>> a = 1
>>> a is True
False
>>> a = True
>>> a is True
True
In my opinion, the second snippet is a bit more readable (if you understand how is
works). With Python 3.8, I get a SyntaxWarning
when executing the second snippet. Would the second example work with all Python implementations? Is there a guarantee that bool
objects of the same value are identical in this way? I believe I have read somewhere that this is not guaranteed for int
objects. Is it ever ok to check literal values with is
like this?