== and is in python

is checks that both operands are the same object. == calls __eq__() on the left operand, passing the right. Normally this method implements equality comparison, but it is possible to write a class that uses it for other purposes (but it never should).

Note that is and == will give the same results for certain objects (string literals, integers between -1 and 256 inclusive) on some implementations, but that does not mean that the operators should be considered substitutable in those situations.


  • '==' checks for equality,
  • 'is' checks for identity

See also

Why does comparing strings in Python using either '==' or 'is' sometimes produce a different result?

Tags:

Python