100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Python

Default and Keyword Arguments in Python

How to give function parameters default values and call functions with keyword arguments, plus the notorious mutable default argument trap.

FunctionsIntermediate9 min readJul 7, 2026
Analogies

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

python
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

python
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

text
['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

Was this page helpful?

Topics covered

#Python#PythonProgrammingStudyNotes#Programming#DefaultAndKeywordArgumentsInPython#Default#Keyword#Arguments#Syntax#StudyNotes#SkillVeris