Why in Python it is better to check None using is 🐍
In Python, you should not write obj == None, even if sometimes it works the same ⚠️
The reason is that == calls the comparison method eq, which can be overridden in the class — and then the behavior becomes unpredictable 🎲
For example:
Here obj == None gives a false result due to custom logic 🤔
Instead:
obj is None
is checks the identity of the object and cannot be overridden. Since None is a singleton, such a check is always correct and predictable ✅
Conclusion: to check for None always use is None — it is the right and safe approach 🛡️
✨ Join Best TG Channels https://t.iss.one/addlist/0f6vfFbEMdAwODBk
⭐️ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A
#Python #Programming #Coding #SoftwareDevelopment #TechTips #DevCommunity
In Python, you should not write obj == None, even if sometimes it works the same ⚠️
The reason is that == calls the comparison method eq, which can be overridden in the class — and then the behavior becomes unpredictable 🎲
For example:
class Weird:
def eq(self, other):
return True # always says "equal"
obj = Weird()
print(obj == None) # True
print(obj is None) # False
Here obj == None gives a false result due to custom logic 🤔
Instead:
obj is None
is checks the identity of the object and cannot be overridden. Since None is a singleton, such a check is always correct and predictable ✅
Conclusion: to check for None always use is None — it is the right and safe approach 🛡️
✨ Join Best TG Channels https://t.iss.one/addlist/0f6vfFbEMdAwODBk
⭐️ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A
#Python #Programming #Coding #SoftwareDevelopment #TechTips #DevCommunity
Telegram
AI PYTHON 🌟
You’ve been invited to add the folder “AI PYTHON 🌟”, which includes 14 chats.
❤4