Truthiness Gotchas
PY · Common Mistakes# Falsy: None, False, 0, 0.0, '', [], {}, set()def process(data=None):
# WRONG: if not data
# This also catches empty list [], 0, and ""
if not data:
print("No data") # Triggered by empty list too!
# CORRECT: if data is None
if data is None:
print("Data is None")
process([]) # Might not intend to reject []
process(None)
process(0)No data
No data
Data is None
No dataNote Empty collections, zero, empty string, and None are all falsy. When you specifically mean 'no value provided', check 'is None' rather than relying on truthiness.