1. Introduction
Default arguments let you specify a fallback value for a parameter so callers can omit it entirely. Keyword arguments let callers specify values by parameter name rather than position, improving readability and allowing arguments to be passed in any order. Together they make function calls more flexible and self-documenting.
Cricket analogy: A bowler's default field setting (a fallback slip cordon) lets the captain skip specifying it every over, while calling out fielders by name ("point, cover, mid-on") rather than position number makes instructions clearer and order-independent.
Default values are evaluated exactly once, at function definition time, not every time the function is called. This single fact is the source of one of Python's most common bugs.
Cricket analogy: A default value evaluated once at definition time is like a pre-season squad photo used all year regardless of injuries or transfers—if a mutable default (like a shared kit bag) is used, changes made in one match unexpectedly persist into the next.
2. Syntax
def function_name(a, b=10, c="default"):
return a, b, c
function_name(1)
function_name(1, b=20)
function_name(a=1, c="custom", b=5)
3. Explanation
Parameters with default values must come after parameters without defaults in the function signature. When calling, keyword arguments can be supplied in any order as long as all required (non-default) parameters are satisfied and each keyword matches an actual parameter name.
Cricket analogy: A bowling order must list the strike bowlers before optional death-over specialists, but when a captain briefs the team verbally, roles can be mentioned in any order as long as every mandatory role (opener, keeper) is covered—like required-before-default parameters and flexible keyword calls.
Mutable default argument trap: default values are evaluated ONCE when the function is defined, and that same object is reused on every call. If the default is a mutable object such as a list or dict, mutations made in one call persist into subsequent calls, silently accumulating state. The standard fix is to use None as the default and create a new mutable object inside the function body when the argument is None.
4. Example
def add_item(item, basket=[]):
basket.append(item)
return basket
print(add_item("apple"))
print(add_item("banana"))
def add_item_fixed(item, basket=None):
if basket is None:
basket = []
basket.append(item)
return basket
print(add_item_fixed("apple"))
print(add_item_fixed("banana"))
5. Output
['apple']
['apple', 'banana']
['apple']
['banana']
6. Key Takeaways
- Default values are evaluated once, at function definition time, not per call.
- Never use a mutable object (list, dict, set) as a default argument directly.
- The safe pattern is default=None, then create the mutable object inside the function body.
- Parameters with defaults must follow parameters without defaults in the signature.
- Keyword arguments can be passed in any order and improve call-site readability.
Practice what you learned
1. When is a function's default argument value evaluated?
2. What is the output of calling add_item('apple') then add_item('banana') where def add_item(item, basket=[]) appends and returns basket?
3. What is the recommended way to give a parameter a mutable default value safely?
4. Which function definition is syntactically invalid?
5. What is a benefit of calling a function with keyword arguments?
6. In add_item_fixed(item, basket=None), why check 'if basket is None' inside the function?
Was this page helpful?
You May Also Like
Function Arguments in Python
Understanding positional, positional-only, keyword-only, *args, and **kwargs arguments in Python function signatures.
Functions in Python
How to define, call, and document reusable blocks of code with Python functions, including the def keyword and implicit None return.
Lambda Functions in Python
Writing small anonymous functions with lambda, using them with map/filter/sorted, and avoiding the late-binding closure trap in loops.
Dictionaries in Python
Python's key-value mapping type, covering hashable keys, insertion order guarantees (3.7+), common dict methods, and typical gotchas.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics