Mystery of Mutable Default Arguments in Python
Unicode) are immutable. Objects of built-in types like (
dict) are mutable. A mutable object can change its state or contents and immutable objects cannot. This is a very simple definition of a Mutable and Immutable object in Python. Now coming to the interesting part which is Mutable Default Object in function definitions.
In the above function, I set
def foobar(element, data=): data.append(element) return data
datato a list(Mutable object) as a default argument. Now let’s execute this function -
The output is as expected, Now execute multiple times -
>>> print(foobar(12)) 
>>> print(foobar(22)) [12, 22] >>> print(foobar(32)) [12, 22, 32]
Why is this happening?Python’s default arguments are evaluated once when the function is defined, not each time the function is called. When Python encounters it, the first thing it will do is compile it in order to create a code object for this function. While this compilation step is done, Python evaluates and then stores the default arguments in the function object itself. So, let’s do some introspection, to clear the confusions, I have taken a few lines of code as below-
- Function Before Execution -
After function executes this definition, You can check the default attribute for this function. by using
def foo(l=): l.append(10)
__defaults__: A tuple containing default argument values for those arguments that have defaults, or None if no arguments have a default value.
The result an empty list as the only entry in
>>> foo.__defaults__ (,)
- Function After Execution-
Astonished? The value inside the object changes, Consecutive calls to the function will now simply append to that embedded list object: Let’s execute the function multiple times:
>>> foo() >>> foo.__defaults__ (,)
The reason why this is happening because default argument values are stored in the function object, not in the code object (because they represent values calculated at run-time).
>>> foo() >>> foo() >>> foo() >>> foo.__defaults__ ([10, 10, 10, 10],)
The SolutionNow, the question is how to code
foobarin order to get the behavior that we expected. Fortunately, the solution is straightforward. The mutable objects used as defaults are replaced by None, and then the arguments are tested for None.
So, by replacing mutable default arguments with None solved our problem.
def foobar(element, data=None): if data is None: data =  data.append(element) return data >>> foobar(12) 
Good Use of Mutable Default ArgumentHowever, Mutable Default Argument have some good use-case also as following-
- Binding local variable to the current value of the outer variable in a callback
Please share your Feedback:
Did you enjoy reading or think it can be improved? Don’t forget to leave your thoughts in the comments section below! If you liked this article, please share it with your friends, and read a few more!