Common mistakes about Python default parameter value

I know it's a little bit long title but didn't find a better one :) It's one of the common mistakes a python beginner does and even more experienced programmers can fall into the same mistake so I thought it might be useful if I illustrated that out here so you can know the concept.

you can easily fall into writing code that look like that:

def get_me_bad_list(arg1, myList=[]):  
    return myList

_In [1]_: get_me_bad_list("hello")  
_ Out[1]_: ['hello']

_In [2]_: get_me_bad_list("world")  
_Out[2]_: ['hello', 'world']  

Interesting, no?

The issue here is that the default value for function parameter is evaluated at function definition time (i.e, module import?) and as list objects are mutable they allow you change the value so you keep having the same default object for every function call and you modify it and return it back and even worse the caller can change it and it'll reflect the list inside the function like the following

_In [3]_: x = get_me_bad_list("Assalam")

_In[4]_: x.append("Alaykum")

_In[4]_: get_me_bad_list("Ya Shabab")

_Out[4]_: ['hello', 'world', 'Assalam', 'Alaykum', 'Ya Shabab']  

So, what's the right way to do that? (if you are not doing that intentionally and you just want the plain default function parameters) that you define the initial value inside the body of the function (execution time)

def get_me_good_list(arg1, myList=None):  
    myList = [] if myList is None else myList
    return myList

_In [1]_: get_me_good_list("hello")  
_Out[1]_: ['hello']

_In [2]_: get_me_good_list("world")  
_Out[2]_: ['world']  

and I'll leave the rest of the tests to you, but generally the explanation is that everytime you call the function we create a new list (if you didn't supply one explicitly) and that list is populated and returned so you won't see any nasty behavior.

It's interesting that you see that the deep you understand how the python interpreter scan and execute the code can really affect the way you fall into mistakes because if you are good enough then you will be somehow safe :)

comments powered by Disqus