r/Python • • Jul 22 '21

Resource Do not use mutable objects as default arguments in Python

https://medium.com/geekculture/do-not-use-objects-as-default-arguments-in-python-1c940212db2e
1.3k Upvotes

218 comments sorted by

View all comments

Show parent comments

2

u/diamondketo Jul 22 '21

2

u/Mehdi2277 Jul 22 '21

It being documented doesn't make it explicit within code. It's still reliant on keeping track of what type the object is and is a lot more implicit vs len(arr) == 0. Style wise most places I've worked at tend to avoid using truthy behavior to make code more explicit. In a fully type checked language it's more ok as at least mypy is telling you no in calling it with the wrong type. I haven't worked on a codebase yet that had near complete type hints.

It's also a bad idea if you work with polyglots. It's a behavior that's mostly specific to python so if you work with engineers that mostly use other languages and dip in python it makes things more confusing.