Boolean identity == True vs is True
Asked 07 September, 2021
Viewed 1.9K times
  • 60
Votes

It is standard convention to use if foo is None rather than if foo == None to test if a value is specifically None.

If you want to determine whether a value is exactly True (not just a true-like value), is there any reason to use if foo == True rather than if foo is True? Does this vary between implementations such as CPython (2.x and 3.x), Jython, PyPy, etc.?

Example: say True is used as a singleton value that you want to differentiate from the value 'bar', or any other true-like value:

if foo is True: # vs foo == True
    ...
elif foo == 'bar':
    ...

Is there a case where using if foo is True would yield different results from if foo == True?

NOTE: I am aware of Python booleans - if x:, vs if x == True, vs if x is True. However, it only addresses whether if foo, if foo == True, or if foo is True should generally be used to determine whether foo has a true-like value.


UPDATE: According to PEP 285 ยง Specification:


  

The values False and True will be singletons, like None.

7 Answer