["python"]
Is it safe to replace '==' with 'is' to compare Boolean-values

I did several Boolean Comparisons: >>> (True or False) is True True >>> (True or False) == True True It sounds like == and is are interchangeable for Boolean-values. Sometimes it's more clear to use is I want to know that: Are True and False pre-allocated in python? Is bool(var) always return the same True(or False) with the pre-allocated True(or False)? Is it safe to replace == with is to compare Boolean-values? It's not about Best-Practice. I just want to know the Truth.
It's rare to need to directly compare booleans. If you are doing something like: if some_bool == True: ... ...just change it to: if some_bool: ... No is or == needed. As commenters have pointed out, there are valid reasons to compare booleans. If both booleans are unknown and you want to know if one is equal to the other, you should use == or != rather than is or is not (the reason is explained below). Note that this is logically equivalent to xnor and xor respectively, which don't exist as logical operators in Python. Internally, there should only ever be two boolean literal objects (see also the C API), and bool(x) is True should be True if bool(x) == True for any Python program. Two caveats: This does not mean that x is True if x == True, however (eg. x = 1). This is true for the usual implementation of Python (CPython) but might not be true in other implementations. Hence == is a more reliable comparison.