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

5

u/Pikalima Jul 23 '21 edited Jul 23 '21

I agree with this. As a general rule of thumb, it’s best not to rely on implicit boolean casting if you are specifically testing None. Too many nights spent in the Python debugger caused by one of the many other false-y values in Python sneaking in unexpectedly. Also, another valid way to do this is arr = [] if arr is None else arr.

1

u/jacksodus Jul 23 '21

I would much prefer this over the implicit boolean casting