What is the mutable default argument pitfall in Python?
beginner 0–2 yrs
Default values are evaluated once, when the def statement runs, not on each call. A mutable default such as a list or dict is therefore shared across calls and accumulates state. Use None as the default and create the object inside the function. The same applies to defaults like datetime.now().
Full answer with examples