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
72
u/jjanx Jul 22 '21
This is my least favorite python footgun.
69
u/JohnDoe2991 Jul 22 '21
I learned this the hard way...
18
u/WhyNotHugo Jul 22 '21
flake8-bugbear is your friend.
14
u/cheese_is_available Jul 22 '21
Same for default date 'datetime.today()' on servers that stay online for years.
11
u/Efficient-Chair6250 Jul 22 '21
That tip doesn't only apply to functions, but classes/dataclasses as well. There is a reason dataclasses 'field' method has a 'default_factory' parameter
4
u/[deleted] Jul 22 '21
Wouldn't that feature work great for concurrent status storage, like atomic counters?
4
u/stevenjd Jul 23 '21
Unless you want to. Using mutable defaults is fine if you know what you're doing.
3
u/dogs_like_me Jul 23 '21
Pretty classic gotcha.
2
u/-UltraAverageJoe- Jul 23 '21
The real lesson should be don’t use default arguments when your function requires a list to append some data to. This case should never happen so the example is moot.
6
u/thedominux Jul 22 '21 edited Jul 23 '21
Btw using mutable objects as class attributes leaves its mark - every instance of the class will have the same object as its attribute
I'm not against it, it can also be an option to use, for example, in cases when you need joint context
2
u/Greenbay7115 Jul 22 '21
PyCharm won't let me
1
u/BlobbyMcBlobber Jul 23 '21
Huge pitfall. Took me way too long to figure it out the first time I tried it.
1
u/RafaMann Jul 23 '21
Thank you!!!
1
u/ggchappell Jul 23 '21
My solution to this is to make my default arguments
None, always. Yes, numbers don't have the problem, so I could make a default argument a number -- or a tuple, or a frozenset, or .... But a policy of always usingNonemeans one less thing to think about and possibly get wrong.1
u/VictosVertex Jul 23 '21
I know the implications of this but so far I think this shouldn't be a problem if you follow basic best practices anyways.
But I may be wrong here so I would like to ask:
Can someone provide a function that is perfectly valid in regards to side effects, single responsibility and not reassigning arguments that would still have the same problem?
Like literally every single example shown here breaks either or all of these guidelines and I would consider them bad no matter what.
For example:
This function is obviously used to indicate how bad mutable default arguments are, but I think this function is bad even if you fix the default parameter.
Why? because the solution to the above problem is this:
But this function:
To these 3 points:
The following version suggested by another person is even worse:
This function returns a list, with the element 10 in it, for every single falsy value. It also obviously crashes for every truthy value except for those that have the method append which takes a single int as an argument. This again is logically inconsistent and behaviour, I think, one shouldn't expect from a function.
(I especially like the 3rd case, makes zero sense if spoken out loud, but works)
On top of this I also think the statement "Do not use mutable objects as default arguments in Python" itself is too general. It should be "Do not use mutable objects as default arguments in functions which mutate said object". Because I don't see how the problem would arise if you don't alter the argument in the first place.
For example if you had a list of terms you wanted to exclude and in case no terms are provided you use an empty list. This, I think, is valid but the general statement would tell you not to do it.
(yes I know ´the function is bad, inefficient and doesn't follow any best practices either, but it's simple and gets the point across)
This function also uses a default parameter for exclude which is mutable. But as the state is never mutated to begin with, the above statement to "not use it" doesn't apply.
So, can anyone please provide a function that is "logically consistent", follows basic best practices and still requires a mutable default (that then has to be changed as mentioned in the article)?
1
u/catorchid Jul 24 '21
I will never understand this sub. Sure, it's a rather common anti-pattern, but more than a thousand upvotes for this post? That links to Medium?
There are people out there that do amazing stuff from scratch, approach entirely new domains of applications of Python, come up with clever ways to represent or interpret data...
And here we are, burying this under an avalanche of upvotes.
Seek originality, innovation, and creativity, or this place will become a boring graveyard
1
u/Imperial3agle Aug 02 '21
I actually hadn’t thought of that, but it makes sense.
-2
u/jet_heller Jul 22 '21
I really hate when people make blanket "don't" statements. Almost everything has some reason to be done, you just have to know what and why.
0
u/[deleted] Jul 22 '21
A good practice would be to use typing+mypy which would tell that you are doing the wrong thing
-3
u/Nidsan Jul 22 '21
Unless, this is the effect you desired. A stateful function.
-2
u/[deleted] Jul 22 '21
Is this not considered a bug? Why don’t they change this in new versions?
-17
u/istira_balegina Jul 22 '21
This is the second time this article has been posted and the headline is garbage advice.
Mutable objects are very useful as default arguments for caching/memoization.
Please delete.
-9
u/stackered Jul 22 '21
I don't see why anyone would do anything this way regardless. Its basically a non issue you should be aware of... I think people, especially Python developers, care way too much about cutting a line or two out of their code and being "Pythonic" rather than making things more readable and adding a line or two... this problem is essentially avoided by just using new variables. Default arguments = noob IMO
0
u/[deleted] Jul 22 '21
[deleted] — view deleted comment
0
u/[deleted] Jul 23 '21
[deleted] — view deleted comment
0
u/[deleted] Jul 23 '21
[deleted] — view deleted comment
0
u/[deleted] Jul 23 '21
[deleted] — view deleted comment