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.me/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.me/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 15 chats.
❤6
Do not violate the Single Responsibility Principle 🎯
A function should do one thing, and do it well.
This function does too much:
The problem here is that the calculation of the subtotal, discount, and tax are all combined into one function. Any change to one of these steps can affect the entire calculation.
It's better to break down the logic into smaller, more specialized functions:
This is much better. ✅
Smaller functions with a single task are easier to test with unit tests because they have fewer dependencies and require less mocking.
Furthermore, isolated components are easier to reuse in different parts of the application or pipeline without bringing in unnecessary dependencies.
Therefore, keep your functions simple and focused.
One function – one responsibility. 📝
#Python #Coding #SoftwareDevelopment #CleanCode #Programming #BestPractices
✨ Join Best TG Channels https://t.me/addlist/0f6vfFbEMdAwODBk
⭐️ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A
A function should do one thing, and do it well.
This function does too much:
def calculate_final_total(
price: float,
quantity: int,
discount_rate: float,
tax_rate: float
) -> float:
# Calculate the subtotal
subtotal = price * quantity
# Apply the discount
discounted_amount = subtotal * (1 - discount_rate)
# Calculate the tax
final_total = discounted_amount * (1 + tax_rate)
return final_total
The problem here is that the calculation of the subtotal, discount, and tax are all combined into one function. Any change to one of these steps can affect the entire calculation.
It's better to break down the logic into smaller, more specialized functions:
def calculate_subtotal(price: float, quantity: int) -> float:
return price * quantity
def apply_discount(subtotal: float, discount: float) -> float:
return subtotal * (1 - discount)
def calculate_tax(amount: float, tax_rate: float) -> float:
return amount * (1 + tax_rate)
This is much better. ✅
Smaller functions with a single task are easier to test with unit tests because they have fewer dependencies and require less mocking.
Furthermore, isolated components are easier to reuse in different parts of the application or pipeline without bringing in unnecessary dependencies.
Therefore, keep your functions simple and focused.
One function – one responsibility. 📝
#Python #Coding #SoftwareDevelopment #CleanCode #Programming #BestPractices
✨ Join Best TG Channels https://t.me/addlist/0f6vfFbEMdAwODBk
⭐️ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A
Telegram
AI PYTHON 🌟
You’ve been invited to add the folder “AI PYTHON 🌟”, which includes 15 chats.
❤3