3 ms·
If this is a class, you can use dataclasses.field with default_factory. However I don't think there is anything analogous for standalone functions. https://doc
by kortex 5y ago
If this is a class, you can use dataclasses.field with default_factory. However I don't think there is anything analogous for standalone functions.
https://docs.python.org/3/library/dataclasses.html#dataclasses.field https://docs.python.org/3/library/dataclasses.html#dataclass...
Functions are in fact objects as well, so there might be some way to do this sort of behavior with decorators.
I would actually contend that "if x is None, x =Bar()" is actually a superior pattern in some/many cases. I used to use "x or Bar()" or "x if x is not None else Bar()" quite a lot, due to its compactness. However I learned that actually sometimes code coverage fails to ensure proper coverage of these ternary assignments. If your default argument is just because you need to initialize a mutable object, ternary works fine. However if the presence or lack of a parameter is an important part of your state space, you probably want explicit coverage.
https://stackoverflow.com/questions/47958166/code-coverage-and-ternary-operators https://stackoverflow.com/questions/47958166/code-coverage-a...