Interning

say it: “in-TUR-ning” /ɪnˈtɝnɪŋ/

Python keeps one shared copy of some values, like small numbers and short strings.

What is interning?

Interning means Python keeps a single shared copy of some values, like small numbers and short strings, instead of making a new one each time. It saves memory, and it's why is sometimes seems to work on numbers. Use == anyway.

a = 256
b = 256
print(a is b)   # True: small numbers are shared

x = 1000
y = int("1000")
print(x is y)   # False: not shared
print(x == y)   # True: always compare with ==

Level: Master · More Python terms at this level

Updated