Python Mutable Default Argument Playground

def f(x, lst=[]): when is that default made, and does every call get its own list? Call it repeatedly, watch what it holds. Try the fix with None. An…

Why it matters

This gotcha bites almost every Python beginner, and many old hands. Knowing "defaults are worked out at def time" explains this bug, mutable class attributes, a frozen def f(x=time.time()), and why the None sentinel is everywhere.

The key idea

A default is worked out once, when def runs

Common mistakes

Try it in this playground