mutable defaults - python’s most notorious gotcha.
when you use a mutable object (like a list or dict) as a default argument in a function, it doesn’t reset on every call.
it stays the same across calls. and that freaks people out.
what actually happens?
default arguments in python are evaluated once, at function definition time, not each time the function is called.
so to=[] only gets created once, and all future calls share the same list.
this leads to weird bugs. mutated state. cross-call leakage.
so the community response is almost religious:
never use mutable defaults.
what if we used that behavior … the shared memory between function calls … on purpose?
what if we wrapped it safely, gave it locks, scoped it to the function itself, and allowed it to behave like…
a class?
i call them lambda classes:
functions that remember, communicate, and evolve across time, without ever needing to become objects.