Skip to content

IS

Import packages

from copy import deepcopy

Checking if two variables point to the same object in memory can be done using the is operator. This is different from the == operator, which checks for value equality. The is operator checks for identity, meaning it checks if both operands refer to the same object in memory.

Define a and b

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

a and b are same object in memory?

a is b, id(a), id(b)
(False, 124015494187328, 124015493703040)

c and b have equal values?

c = b
c == b
True

c and b are same object in memory?

c is b, id(c), id(b)
(True, 124015493703040, 124015493703040)

c and d have equal values?

d = deepcopy(c)
c == d
True

c and d are same object in memory?

c is d, id(c), id(d)
(False, 124015493703040, 124015494012416)

Define x and y

1
2
3
x = 2
y = x
x, y
(2, 2)

x and y have equal values?

x == y
True

x and y are same object in memory?

x is y, id(x), id(y)
(True, 107491941438136, 107491941438136)
x = 3
x, y
(3, 2)

x and y have equal values?

x == y
False

x and y are same object in memory?

x is y, id(x), id(y)
(False, 107491941438168, 107491941438136)

Change y

y = 6
x, y
(3, 6)