Differences between is and == in python

    is
    • is is used to check for identity
    • is returns True if both objects are same
    • is not is the counterpart of is
    • is cannot be overloaded
    • Any comparisons to singletons like None are better done with is or is not
    • As a rule of thumb, a is b implies a == b (except for odd things like NaNs)
    ==
    • == is used to check for equality
    • == returns True if both objects have the same value
    • != is the counterpart of ==
    • == can be overloaded
    • Any comparisons to singletons like None are better done with is, not with ==
    • As a rule of thumb, a == b does not necessarily imply that a is b (except for interned objects)
    is is an identity operator and == is a comparison operator; there are few differences between them:
    is is used to check for identity, so it will return True if two variables point to the same object (in memory)
    == is used to check for equality, so it will return True if both objects have the same value
    comparisons to singletons like None are better be done with is not with == as some singletons, though they are same objects will fail equality test (for e.g. nan)
    is cannot be overloaded
    == can be overloaded
    As a rule of thumb, a is b implies a == b (except for odd things like nan)
    a == b does not necessarily imply that a is b (except for interned objects)
    Python 3.9
    a = float('nan')
    b = a
    a is b     # True
    a == b     # False
    
    Python 3.9
    a = 4
    b = 4
    a == b     # True
    a is b     # True
    


    difference-between-is-and-==-in-python
gk