Python Default Parameter Gotcha

  • September 8, 2019
  • Python
Default Parameter Gotchas

Default Parameter Gotchas

Take a look at this code. All it does is return a list with appended parameter. In case no list is supplied, it defaults to an empty list and appends to it. Or is it???

def add_to_list(item, item_list=[]):
    item_list.append(item)
    return item_list

add_to_list("gold") # expected: ["gold"] #actual: ["gold"]
add_to_list("silver") # expected: ["silver"] #actual: ["gold","silver"]

Look at the second result. We expected [“silver”] expecting the item to be appended to an empty list as we never passed in a list and expected a default empty list as in function definition.

But here is the gotcha. Python’s default arguments are evaluated once when the function is defined, not each time the function is called. This means that if you use a mutable default argument and mutate it, you will and have mutated that object for all future calls to the function as well.

Corect Way
Use sentinel objects like None. These are non-mutable. Then incorporate guards to check for sentinels.

def add_to_list(item, item_list=None):
    if item_list is None:
        item_list = []
    item_list.append(item)
    return item_list

add_to_list("gold") # expected: ["gold"] #actual: ["gold"]
add_to_list("silver") # expected: ["silver"] #actual: ["silver"]
Tek Shinobi
Author: Tek Shinobi

Hiya Ninjas, I am the ninja who invented the fire, wheel, science, technology and everything intelligent this humanity has ever experienced since its evolution from monkeys and germs. Actually, above is a partial list. I also was involved with the Big Bang that created this universe. What was it like before the Big Bang is so secret that if I tell you, I will have to turn you into a hobbit and force you to be my gardener. Okay. Sayonanra, Namaste Tek Shinobi




No Comments


You can leave the first : )



Leave a Reply

Your email address will not be published. Required fields are marked *