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

3

u/stevenjd Jul 23 '21

It's great to see Redditors and their teams still using bug magnets from the dark days of Python 1.5 in their code in 2021, fifteen years after conditional expressions were added to the language.

(Presumably just to save a few characters.)

Never change Reddit!

1

u/Smallpaul Jul 23 '21 edited Jul 23 '21

The bug you linked to is in a completely irrelevant context to the current discussion.

Raymond wanted Boolean behaviour and he got full type behaviour. We are using the operator FOR its full type behaviour as it was designed.

In other words, the situation is the exact opposite.

If this convention has not caused a bug in my code in 15 years, or my current team’s code in 5 years, what evidence is there that it is error prone? Can you give an example of where using it for cleaning up missing defaults causes bugs.

Sure, or and and can cause bugs. But not when they add used like this.

Every language feature can be buggy when used in certain contexts. You might as well say that lists should never be used due to the risk of mutable default arguments.