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.
# list.py
from typing import List
class A(object):
def __init__(self, lst: List[int] = [0]):
self.lst = lst
print("-------------")
lst = [1,2,3]
a = A()
print(a.lst)
a.lst.extend(lst)
print(a.lst)
print("-------------")
lst = [1,2,3]
a = A()
print(a.lst)
a.lst.extend(lst)
print(a.lst)
print("-------------")
lst = [1,2,3]
a = A([0])
print(a.lst)
a.lst.extend(lst)
print(a.lst)
print("-------------")
lst = [1,2,3]
a = A()
print(a.lst)
a.lst.extend(lst)
print(a.lst)
print("-------------")
When we run the program, however, the result is somehow “unexpected”.
$ python list.py
-------------
[0]
[0, 1, 2, 3]
-------------
[0, 1, 2, 3]
[0, 1, 2, 3, 1, 2, 3]
-------------
[0]
[0, 1, 2, 3]
-------------
[0, 1, 2, 3, 1, 2, 3]
[0, 1, 2, 3, 1, 2, 3, 1, 2, 3]
-------------
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.:”
def whats_on_the_telly(penguin=None):
if penguin is None:
penguin = []
penguin.append("property of the zoo")
return penguin
Therefore, the “correct” of implementing class with List
object argument is
# list.py
from typing import List, Optional
class A(object):
def __init__(self, lst: Optional[List[int]] = None):
if lst is None:
self.lst = [0]
else:
self.lst = lst
print("-------------")
lst = [1,2,3]
a = A()
print(a.lst)
a.lst.extend(lst)
print(a.lst)
print("-------------")
lst = [1,2,3]
a = A()
print(a.lst)
a.lst.extend(lst)
print(a.lst)
print("-------------")
lst = [1,2,3]
a = A([0])
print(a.lst)
a.lst.extend(lst)
print(a.lst)
print("-------------")
lst = [1,2,3]
a = A()
print(a.lst)
a.lst.extend(lst)
print(a.lst)
print("-------------")
Now the result is expected.
$ python list.py
-------------
[0]
[0, 1, 2, 3]
-------------
[0]
[0, 1, 2, 3]
-------------
[0]
[0, 1, 2, 3]
-------------
[0]
[0, 1, 2, 3]
-------------
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.