AI engineering glossary · Python core

What is mutable default argument?

Mutable default argument: A well-known pitfall where a mutable object such as a list is used as a default value. Because defaults are created once at definition time, the same object is shared across calls and accumulates changes. The safe pattern is to default to None and create the list inside.

Level: intermediateTopic: Python core

Mutable default argument: an example

def add(x, items=[]): items.append(x); return items returns [1] then [1, 2] on successive calls. Use items=None instead.

Related terms

More Python core terms

Where you learn it

Mutable default argument is part of the AI Engineering Program, which teaches it with notes, a hands-on lab and an exam. The first lesson is free.

Before you go

Take the cheat sheet with you

One free PDF: the key AI-engineering terms, formulas and Python idioms. No spam; unsubscribe any time.