r/PythonLearning • u/ETL_Pipeline_Dev • 1d ago
Discussion Where do you use None in python
So far I've only really used None in two places:
- With dict.get(), when the key isn't there.
- With del in classes. I set a variable to None so the object gets cleaned up.
Where else do you use it? Do you use it as a default value for function arguments, or for missing data?
I'm curious how people use it in real projects.
14
Upvotes
View all comments
4
u/Alexku66 1d ago
It's kinda everywhere. Each sentinel like `if not items: return` in fact returns None. When function modifies object in-place, or does something without the purpose to return anything, in fact it returns None. Each time you declare variable before using it in a loop, you assign None value first.
More than that, sometimes one None is not enough. When write API that updates existing object, say product in database, which description column is nullable. If you provide {"description" : None} , does it mean you want description to be empty (null) or to not update it? I usually create another class, UNSET, to explicitly state that this attribute is not updated