6.11. Boolean Identity

6.11.1. Rationale

  • = assignment

  • == checks for object equality

  • is checks for object identity

6.11.2. Identity Check

  • is checks for object identity

  • is compares id() output for both objects

  • CPython: compares the memory address a object resides in

  • Testing strings with is only works when the strings are interned

Since Python 3.8 - Compiler produces a SyntaxWarning when identity checks (is and is not) are used with certain types of literals (e.g. str, int). These can often work by accident in CPython, but are not guaranteed by the language spec. The warning advises users to use equality tests (== and !=) instead.

>>> name = None
>>>
>>> name is None
True
>>> name is not None
False

6.11.3. Bool Identity

>>> name = None
>>>
>>> name is None
True
>>> name is False
False
>>> found = True
>>>
>>> found == True
True
>>> found is True
True

6.11.4. String Identity

>>> a = 'Mark Watney'
>>> b = 'Mark Watney'
>>>
>>> a == b
True
>>> a is b
False
>>> 'Mark Watney' is 'Mark Watney'  
<...>:1: SyntaxWarning: "is" with a literal. Did you mean "=="?
True

6.11.5. Control Flow

>>> name = None
>>>
>>> if name is None:
...     print('Name is empty')
Name is empty

6.11.6. Assignments