Today, I wanted to share a quick life-saving advice about a common mistake Python developers still tend to make.
TL;DR
Do not use mutab...
For further actions, you may consider blocking this person and/or reporting abuse
Would you consider
a viable alternative to your if-construction?
Yes, I would see that as a viable alternative :-) — although I would argue that:
if X is None
check in Python code more often — not sure it means anything in terms of "pythonicism", though!if X is None
anyway.We have a static check to avoid these kind of issues
pylint -d all -e dangerous-default-value edi_expense.py
The output will be:
(dangerous-default-value) Dangerous default value [] as argument
You can use pylint in your CI in order detect them early
Another option is using a non mutable type like a tuple
Yep, that would solve it if your argument can be transformed to use a non-mutable version.
However, Python is already so confusing on types that I wouldn’t recommend providing a tuple if you’re to use a list afterwards.
I like to use type annotations, so annotating an argument as a
List
while providing an empty tool for the default would look suspicious. ;)What do you think about