DEV Community

Cover image for Equality by Value vs Equality by Identity
icncsx
icncsx

Posted on

Equality by Value vs Equality by Identity

There’s a difference in meaning between identical and equal. This distinction is the same distinction between is and == operator in Python.

  • The == operator compares by checking for equality of values.

  • The is operator, however, compares by checking for equality of identities (they point to the same memory address).

Suppose you have two lists.

a = [1, 2, 3]
b = [1, 2, 3]

a is b # False (check equality by identity)
a == b # True (check equality by value)
Enter fullscreen mode Exit fullscreen mode

One way to determine whether a is b is by using the built-in id function which will return the memory address that the variable name points to.

id(a) == id(b) # False
Enter fullscreen mode Exit fullscreen mode

Top comments (0)