r/Python • u/[deleted] • 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
r/Python • u/[deleted] • Jul 22 '21
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 isarr = [] if arr is None else arr.