Open Metric
Science, Fiction and Me

Today I Learned

Python Default Parameters Tripped Me Up

As we might use default parameters in python functions, such as

def func(inp = []):
    inp.append(1)
    return inp

this might cause trouble. Since python functions are objects of a certain identity no matter how many times you run it, the input inp would be changed with each run.

The better strategy is to use None.

def func(inp = None):

    if inp is None:
       inp = []

    return inp

For references, please read: Default Parameter Values in Python.

A Random #TIL# for You


Most technical TILs will be updated at datumorphism.leima.is/til

By OctoMiao

Last updated