Default arguments are evaluated once, when the function is defined, not on each call. A default list or dict is therefore a single shared object that persists across every call, so mutations accumulate and leak between callers. The fix is to default to None and create a fresh container inside the body when the argument is None.
Why interviewers ask this
This is a quick check on whether you understand that a def statement executes and binds objects at definition time. Interviewers like it because the wrong answer is confident and specific: people insist the list is recreated per call. It also opens the door to talking about the difference between binding a name and copying a value, which underpins a lot of Python surprises.
How to structure your answer
- State that defaults evaluate once at definition time.
- Explain the shared object consequence.
- Give the None sentinel fix.
- Note that immutable defaults are safe.
Example answer
Because the default object is created once, when Python executes the def statement, and then every call that omits the argument gets that same object. If it is a list and the function appends to it, call three sees what calls one and two left behind. I hit this on a caching helper that took a default empty dict of options. Under a long lived worker the dict grew for days and eventually a request inherited settings from an unrelated tenant, which is not a fun bug to explain. The fix is boring: default to None, then inside the body check explicitly for None and build a fresh dict, so an empty dict passed in on purpose is still respected. Immutable defaults like integers, strings, or None are fine because nothing can mutate them. Ruff flags this as B006 out of the box, so it is one of those bugs that should never reach review in a project with linting wired into CI.
Walking into this interview soon? GhostPilot listens to your live call, spots the question the moment it is asked, and puts a structured answer on your screen in real time. Try it on your next mock, or grab a $29 Session Pass, no subscription, for the real thing.
See how it worksFollow-up questions to expect
- Why is using or instead of an explicit None check risky?
- Are tuples safe as defaults?
- Where else does definition time evaluation surprise people?
Related python developer questions
Your interviewer will ask their own version of this. Paste your actual job description into the free Question Predictor and get the 20 questions that role is most likely to ask, with what each one is really probing.
Predict my questions