Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Mutable default

The default list assigned to b is created when the f functions is defined. After that, each call to f() (that does not get a "b" parameter) uses this common list.

def f(a, b = []):
    b.append(a)
    return b

print(f(1))
print(f(2))
print(f(3))

{% embed include file="src/examples/advanced-functions/mutable_default_parameter.out)

Use None instead: