Python & FastAPI•Part 2: Python Core Language, Internals & Runtime (6 - 25)
`is` vs `==` and Integer Caching:
In Simple Words:
==: Compares values (a == b).is: Compares memory identity (id(a) == id(b)).- Small Integer Caching: CPython pre-allocates integers between
-5and256, soa = 100; b = 100; a is bisTrue.
python
a = 256
b = 256
print(a is b) # True (cached by CPython runtime)
x = 1000
y = 1000
print(x == y) # True (same values)
print(x is y) # False (different objects in memory)