Search code examples
pythonequalitynonetype

Since when is Python None equal to None?


I always thought that Python nulls are not equal, as is common in many other languages and based on simple logic (if the value is unknown, how can it be equal to another unknown?).

However, recently I tried it, and discovered that:

Python 3.10.2
>>> None == None
True

Has it always been this way? If not, which version changed it?


Solution

  • None has always been equal to itself in python. "None == None" has always evaluated as true.

    As seen from this documentation https://docs.python.org/3/c-api/none.html

    Since None is a singleton, testing for object identity (using == in C) is sufficient.