Python Mutable Object as Default Argument
Introduction
Python default arguments are very convenient for object initialization and function calls. However, when the default argument is a List
or any other mutable object, often the time the behavior of the object or the function will not match our expectation. Recently, I got bitten by this pitfall again. Therefore, I would like to write a blog post on this so that I will not fall again in the future.
Example
We have the following implementation for class A
which initialize its member variable self.lst
with a List
object.
1 | from typing import List |
When we run the program, however, the result is somehow “unexpected”.
1 | $ python list.py |
We could see that even if we have initialized new instance of class A
, the initialized member variable self.lst
is not the default value [0]
when we create a new object of A
and run some simple stuff multiple times.
From Python’s official documentation, Chapter 8.6. Function definitions, it explicitly stated the pitfall.
“Default parameter values are evaluated from left to right when the function definition is executed. This means that the expression is evaluated once, when the function is defined, and that the same “pre-computed” value is used for each call. This is especially important to understand when a default parameter is a mutable object, such as a list or a dictionary: if the function modifies the object (e.g. by appending an item to a list), the default value is in effect modified. This is generally not what was intended. A way around this is to use None as the default, and explicitly test for it in the body of the function, e.g.:”
1 | def whats_on_the_telly(penguin=None): |
Therefore, the “correct” of implementing class with List
object argument is
1 | from typing import List, Optional |
Now the result is expected.
1 | $ python list.py |
Final Remarks
This caveat not only applies to List
objects but also other mutable objects serving as default arguments in Python. The best practice is, probably, not to use default arguments for arguments that are of mutable types.
References
Python Mutable Object as Default Argument
https://leimao.github.io/blog/Python-Default-Argument-Mutable-Object/