Jegan Sekar’s Post

🐍 This Python behavior confused me for a long time. I saw this code: a = 256 b = 256 print(a is b) It printed: True ✅ Then I changed just ONE thing: a = 257 b = 257 print(a is b) Suddenly: False ❌ Same value. Same code. Different result. What’s going on? 🤯 🧠 The reason: Python object caching. Python pre-creates and reuses small integers (usually from -5 to 256). So when you write: a = 10 b = 10 Both variables point to the SAME object in memory. But beyond that range? New objects get created. 💡 The real lesson: ✨ `is` checks identity, not value ✨ `==` checks value equality ✨ Small details matter in backend systems I’ve seen real bugs caused by this misunderstanding. Now I follow one rule: 👉 Use `==` for values 👉 Use `is` only for None, True, False Python feels simple… until you look a little deeper 👀 And that’s where good engineers are made. #Python #BackendDevelopment #SoftwareEngineering #ProgrammingTips #DeveloperLearning #TechExplained

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories