Skip to main content

Mutable default arguments and evaluation timing

Why is def append_item(x, items=[]): a classic Python bug, and how do you fix it?

Official Reference

Model Answer & Explanation

Default argument expressions are evaluated once when the def statement executes, not on each function call. Every call without an explicit items argument shares and mutates the exact same list object. Fix it by defaulting to None and initializing items = [] inside the body.

Follow-up Interview Question · What is the equivalent pitfall inside a @dataclass with list fields?

Module Context & References

Python & FastAPI · Popular interview theory The 20% of Python & FastAPI concepts asked in 80% of backend interviews
Personal Notes · Markdown

Technologies & Interview Topics Covered

Switch between short-term interview tracks and long-term 80/20 engineering depth to practice JavaScript internals, React rendering and hooks, timed frontend machine coding UI challenges, MeetaPro FG300, Blind 75 & NeetCode 150 DSA patterns, Python & FastAPI APIs, PostgreSQL query optimization & MVCC, Redis caching, Applied AI RAG pipelines & evaluations, and distributed system design.