1.2.28 · D1Introduction to Programming (Python)

Foundations — Default parameters, keyword arguments

1,566 words7 min readBack to topic

Before you can safely use defaults and keyword arguments, six small things must be crystal clear. This page builds each from absolute zero, in the order they depend on each other. Nothing here assumes you already know the parent topic — we are laying its floor.


1. What a variable is (a labelled box)

Figure — Default parameters, keyword arguments

WHY the topic needs this: a parameter is nothing more than a special variable that gets its box filled at the moment the function is called. If "name attached to a value" is fuzzy, "parameter" and "argument" will be fuzzy too.


2. What a function is (a machine def builds)

def area(length, width):     # 'area' is the name; length, width are the knobs
    return length * width     # the steps the machine runs

The full grammar of def, return, and calling lives in Functions - def and return — here we only need the two round-bracket pieces: what goes inside ( ) at definition, and what goes inside ( ) at the call.


3. Parameter vs argument (the knob vs the setting)

This is the single most important distinction on the whole page, and beginners blur it constantly.

Figure — Default parameters, keyword arguments

4. The = sign wears two different hats

The symbol = is used for two totally different jobs, and confusing them causes real errors.

Figure — Default parameters, keyword arguments

WHY the topic needs this: the entire parent topic is built on name=value written inside brackets. If you read def f(x=10) as "assign 10 to x right now" you will misunderstand when the default is created — the root of the famous mutable-default bug. The correct reading is "if the caller gives nothing for x, use 10."


5. None and the words is / if (the "was nothing given?" test)

def collect(x, seen=None):     # blank marker instead of a list
    if seen is None:           # was nothing given?
        seen = []              # then make a FRESH box now
    seen.append(x)
    return seen

WHY the topic needs this: None is the standard sentinel — a stand-in that says "the real default should be built fresh, at call time." Without understanding None, is, and if, the correct fix for the mutable-default trap is unreadable.


6. Mutable vs immutable (why one shared box bites you)

Figure — Default parameters, keyword arguments

WHY the topic needs this: a default value is created once, when the def line runs. If that default is a mutable list, every call that omits the argument shares the one list — the figure's two arrows point at one box, so appends pile up across calls. If the default were immutable (like a number), no such sharing could hurt you. This is the single deepest idea behind the parent's biggest warning. Full treatment: Mutable vs immutable objects.


Prerequisite map

Variable - name on a box

Function built with def

Parameter vs Argument

The = sign two meanings

Default parameter

Keyword argument

None sentinel with if and is

Mutable-default fix

Mutable vs immutable

Default params and keyword arguments

Each foundation feeds the one to its right; together the three streams — what a knob is, what the = tag does, and why a shared box is dangerous — meet at the parent topic. See also DRY principle - Don't Repeat Yourself (why defaults exist at all), Variable scope - local vs global (where these names live), and *args and **kwargs (variadic functions) (the next step beyond fixed knobs). Return to the parent: the topic note.


Equipment checklist

A variable is best pictured as...
a name/label pointing at a box that holds one value.
def is short for...
define — it builds a new function (machine) with named knobs.
A parameter is...
a name inside the brackets of the def line — an empty box waiting to be filled.
An argument is...
the actual value passed inside the brackets when you call the function.
Inside a def line, x=10 means...
give parameter x a default value of 10, used only if the caller omits it.
Inside a call, x=10 means...
a keyword argument — dial the knob named x to 10, regardless of order.
== differs from = because...
== asks "are these equal?" (True/False); = sets or tags a value.
None means...
a deliberate "nothing / blank here" value, used as a sentinel.
x is None checks...
whether x is the very same object as None — i.e. "was nothing given?".
A mutable value is one that...
can be changed in place (lists, dicts); two labels on it see each other's edits.
A default value is created...
once, when the def line runs — not on every call.