Why does my is comparison fail with simple integers?

Gihin Oha
Member
Joined: 2025-02-16 08:26:47
2025-02-16 08:52:38

Hello all,
I’m running into a weird issue. I have this code where I’m comparing integers using the is operator:

a = 1000 b = 1000 print(a is b)

I expected the result to be True, but it’s actually False. From what I’ve learned, is checks if two variables point to the same object in memory, so I thought integers like 1000 would be cached and reused.

Can anyone explain why this happens? Should I use == for integer comparisons instead of is?