What is a default parameter — and why in Python it can 'remember' the last call

Here's a Python function. It adds an item to a cart, and the cart is empty by default:
def add(item, cart=[]):
cart.append(item)
return cart
add("bread") # ['bread']
add("milk") # ['bread', 'milk'] — where did the bread come from?!
The second call didn't pass a cart. It should have got a fresh empty one. Instead it got yesterday's bread. This isn't a Python bug — it's how default parameters work. Let's figure out what they are and where else they bite.
What a default parameter is
A function takes parameters — the input it works with. A default parameter is a parameter with a fallback value. Pass your own and the function uses yours. Don't pass one and it uses the fallback.
Like a coffee machine with a standard cup already under the spout. Bring your own mug and it pours into that. Don't — it pours into the standard one.
function greet(name = "friend") {
return "Hi, " + name;
}
greet("Anna"); // "Hi, Anna"
greet(); // "Hi, friend"
Why bother? So you don't repeat the same thing on every call. A loading function can fetch 20 records by default, a rounding function can keep two decimals. Whoever needs something else passes their own.
Trap #1: Python creates the value once
Back to the cart. Python evaluates the default value once — when the function is defined, not on every call. The empty list is created a single time and then lives inside the function.
Every call without a cart gets the very same list. The first one put bread in it. The second got the list with bread and appended milk. We ran this in Python 3 — that's exactly what happens.
Why it works this way is covered in detail in the piece on variables: a name in Python is a tag on a value, not a box. All the calls hold a tag on one and the same list.
The standard fix — use None as the fallback and create the new list inside:
def add(item, cart=None):
if cart is None:
cart = []
cart.append(item)
return cart
The Python rule: defaults must be immutable. Numbers, strings, True/False, None — fine. Lists, dicts, sets — go through None.
Trap #2: JavaScript lets null through
JavaScript doesn't have the first trap: the default is created fresh on every call. It has a different one instead.
The default kicks in only when undefined arrives. Not null, not 0, not an empty string:
function limit(count = 20) {
return count;
}
limit(); // 20
limit(undefined); // 20
limit(null); // null — the fallback did NOT kick in
limit(0); // 0
Where it bites: data came from the server, the field was empty, and the server sent null. You counted on the fallback of 20. The function got null instead, and everything downstream goes sideways. We covered how null differs from undefined in what is null.
If null should also mean "use the fallback", say so explicitly inside the function: count = count ?? 20. The double question mark replaces both null and undefined.
Handy details
A default can lean on the parameter to its left:
function frame(width, height = width) { /* ... */ }
frame(100); // a 100×100 square
Defaults go at the end of the list. If the first parameter has a fallback and the second doesn't, the only way to skip the first is to pass undefined by hand. Awkward and hard to read.
And if more than two parameters have fallbacks, it's easier to pass a single settings object. Then you only specify what you change and don't have to remember the order.
Where you'll meet this in AI-written code
All the time. Models happily write def fetch(url, headers={}) or function load(options = {}). In JavaScript the second one is safe. In Python the first one is the classic trap — a linter flags it, human eyes skip right over it.
When you review AI-written code, look for =[], ={} and =set() in the parentheses after a Python function name. Found one — ask to switch it to None. A one-line edit that saves an evening of debugging.
Is a default parameter the same as an optional parameter?
Almost. A parameter with a default automatically becomes optional: you can leave it out. TypeScript also has optional parameters without a fallback — inside the function they're simply undefined.
Can a default be the result of a function call?
Yes. In JavaScript the expression is evaluated on every call, for example created = Date.now(). In Python it's evaluated once at definition, so the time gets "frozen". There, too, people use None and compute it inside.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





