Skip to content
Minute Campus.

Almost rightLesson 091 min

This function remembers things it should have forgotten

A function with an empty list as a default value remembers items between calls. Python builds that list once, when the function is defined, so every call that leaves the argument out shares it. One customer's apple then turns up in the next customer's basket. The fix is to default to nothing and create a fresh list inside the function.

Remember

Never use a list or a dictionary as a default value.

Sources

Checked at the source.

Every claim in the lesson was checked against these before it was recorded.

  1. 1More on Defining Functions, in the Python TutorialPython documentation · read 4 Oct 2026The default value is evaluated only once, which matters when it is a mutable object such as a list.

Transcript

The lesson, word for word.

Read it at your own pace, or search the page for a term.

This function remembers things it should have forgotten. Three seconds: why? It adds an item to a basket. If you do not pass a basket, it starts with an empty one. Call it once with an apple. You get a basket with an apple. Call it again with a pear, for a different customer. You get an apple and a pear. The first customer's apple is still there. Here is why. Python builds that empty list once, when the function is defined, not each time it is called. Every call that leaves the basket out shares the same list. Python's own tutorial flags this with the words: important warning. The fix: make the default nothing at all, and create a fresh list inside the function. The rule: never use a list or a dictionary as a default value. Test a function by calling it twice, not once.

Questions? Comment on the lesson on YouTube

New lessons every day.

Follow Minute Campus where you already scroll.