You should use these comparisons for their semantics.
Use is
to check identity and ==
to check equality.
In Python names refer to objects, for example in this case value1
and value2
refer to an int
instance storing the value 1000
:
value1 = 1000
value2 = value1
>>> value1 == value2
True
>>> value1 is value2
True
In the following example the names value1
and value2
refer to different int
instances, even if both store the same integer. Because the same value (integer) is stored ==
will be True
, that's why it's often called "value comparison". However is
will return False
because these are different objects:
>>> value1 = 1000
>>> value2 = 1000
>>> value1 == value2
True
>>> value1 is value2
False