SpinTheMic
Pro

Interview practice question

Python function remembers old values

Backend Engineering · Language Runtimes · Beginner

A Python function def add_item(item, items=[]) keeps accumulating items across calls. Why?

What a strong answer covers

Your AI report card counts how many of these ideas you cover.

  • Default evaluated once at definition.
  • Same list object shared between calls.
  • Use None as default, create inside.
  • Applies to dicts, sets too.
  • Linters warn about it.
Practise this topic

Start without signing up. Sign in free to get an AI report card on your answer.

See all topics