Default Parameters
Concepts covered: Default Parameters
A default parameter carries a value written into the def line. When a call leaves that argument out, Python uses the written value. When the call supplies one, the supplied value wins. Pipeline helpers rely on this: most loads append and allow 3 retries, so those become the defaults, and the rare job that needs something else says so at the call.
def load_table(table, retries=3, mode="append"):
print(table, "retries:", retries, "mode:", mode)
load_table("orders")
load_table("orders", 5)
load_table("orders", mode="overwrite")
load_table("orders", 1, "overwrite")Output:
orders retries: 3 mode: append orders retries: 5 mode: append orders retries: 3 mode: overwrite orders retries: 1 mode: overwrite
In the second call, 5 lands in retries because it is the second argument. The third call names mode so it can change mode without touching retries, and retries keeps its default. The fourth call sets both by position. Only table is required, so load_table() with no arguments fails with TypeError: load_table() missing 1 required positional argument: 'table'.
Required parameters come first
Writing def load_table(retries=3, table): stops Python at the def line with a SyntaxError, before any call runs. Its message reports a parameter without a default placed after a parameter with one. The rule protects left-to-right filling. In load_table("orders") the single argument would go to the first parameter, retries, and table would get nothing. With every required parameter ahead of every defaulted one, a positional call always has an unambiguous match.
A default is computed once
The expression after = runs once, when Python executes the def statement, and its result is stored with the function. Calls reuse the stored value and never evaluate the expression again. In this example the default reads a top-level setting that changes before the first call:
retry_delay = 30
def fetch(source, delay=retry_delay):
print("fetching", source, "retry delay", delay)
retry_delay = 60
fetch("orders_api")
fetch("users_api", retry_delay)Output:
fetching orders_api retry delay 30 fetching users_api retry delay 60
The first call prints 30 although retry_delay is 60 by then, because delay captured 30 when the def ran. The second call passes the current value itself and sees 60. The rule causes worse bugs when the default is a list or dict. Python builds that one object at definition time and every call that falls back to the default shares it, so whatever one call adds to it is still there in the next.
def collect(row, batch=[]):
batch.append(row)
return batch
print(collect("a"))
print(collect("b"))
print(collect("c", []))
print(collect("d"))Output:
['a'] ['a', 'b'] ['a', 'b'] ['c'] ['a', 'b', 'd']
.append(row) adds row to the end of the list. The second call passed only "b" and got back ['a', 'b'], because both calls appended to the single list Python built when the def ran. In a pipeline, that is the second batch quietly carrying the first batch's rows. The third call passes its own empty list, so batch is that new list and the shared one is left alone. The fourth call leaves batch out again and gets ['a', 'b', 'd']: the shared list was waiting with the earlier rows still in it.
None as the not-given signal
When the fallback has to be read at call time, set the parameter's default to None and fill in the real value inside the function:
retry_delay = 30
def fetch(source, delay=None):
if delay is None:
delay = retry_delay
print("fetching", source, "retry delay", delay)
retry_delay = 60
fetch("orders_api")
fetch("users_api", 5)
fetch("health_check", 0)Output:
fetching orders_api retry delay 60 fetching users_api retry delay 5 fetching health_check retry delay 0
None works as the marker because no caller means it as a real delay. The test is is None, which matches None and nothing else, so the health check that asks for 0 seconds gets 0 instead of the 60-second fallback. The same pattern fixes collect: default batch to None, and under the is None check write batch = [], so every call that leaves batch out builds a new empty list of its own.
What does this program print?
limit = 10
def fetch_rows(rows, cap=limit):
if rows > cap:
rows = cap
print(rows)
limit = 50
fetch_rows(30)- It prints 30, because cap reads limit during the call and finds 50
- It prints 10, because cap got its value when the def statement ran
- It prints 50, because the latest value of limit is copied into rows
- It raises TypeError, because cap is missing when the call passes 1 argument
- Do
- Put every required parameter before the first defaulted one
- Choose defaults that match what most calls need
- Default to None when the fallback must be read at call time
- Pass a rarely changed option by keyword, as in mode="overwrite"
- Avoid
- Don't default a parameter to a list or dict, which every call shares
- Don't expect a default to follow later changes to a global
- Don't use a real value such as 0 as the not-given marker
About this lesson
This section is part of the Functions: Intermediate lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.