Beware of iterable default params in Python

Somi András | October 29, 2017

I got a question in this Python assessment quiz the other day, that surprised me:

I picked this as an answer:


but the right one was:

[1, 2]
[1, 2, 3]

So, the keyword param items retains its previous value, even though the function f is called repeatedly. This is not what I would intuitively expect from it.

I don’t know about every other languages, but I knew that in JavaScript a similar code would work as I expected. So this:

function f (a, items=[]) {
  return items


would gracefully output this:


It turns out, MDN explicitly calls out Python when explainig JS default params.

The default argument gets evaluated at call time, so unlike e.g. in Python, a new object is created each time the function is called. (Default parameters - MDN)

I found the relevant section in the Python docs too, which warns about this behaviour. Strangely enough it also admits that this is not what was intended, and proposes a ‘workaround’ (which in turn appears all over the place in Python libraries):

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 (The Python Language Reference - emphasis is mine)

Header photo by Keith Misner/ Unsplash