Python Mutable Default Argument Pitfall
Multiple Choice Medium
Question
What happens with `def f(x=[])` called as `f(); f()` twice?
Hint
Default arguments are evaluated once at function definition time.
Explanation
Mutable default arguments are evaluated once when the function is defined, not each call. So the same list object is shared across all calls that use the default. This is a classic Python pitfall — use None as a sentinel and create the list inside the function instead.
Related Resources
Related Lessons
- Python Functions
Default argument pitfalls.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.