4 ms·
"I hate having to have a block of `if bar is None` at the top of my functions." Why? This is like someone saying "I don't like declaring my variables in C at t
by wheelerof4te 5y ago
"I hate having to have a block of `if bar is None` at the top of my functions."
Why? This is like someone saying "I don't like declaring my variables in C at the top of the function".
It is a good and readable solution to a somewhat rare problem. Passing mutable objects as default function parameters is a common code smell in Python.
And no, adding a special syntax to an already syntax-heavy language to address this is not good.
- softwaredoug 5y agoBecause it’s harder for the user of the API to know what the default is. It’s boilerplate that distracts from the purpose of the function, this getting in the way of readability. It’s also just more code prone to a programmer screwing up from time to time, despite how simple it is.
- wheelerof4te 5y agoPythonic way to solve this problem would be to make Bar() a component of another class. Functions are not a good tool for storing stateful objects in Python. That's why classes exist.