Understanding Different Types of Arguments in Python Function Definitions
This article explains Python function definitions, covering required, default, keyword, positional, variable-length (*args, **kwargs), and positional-only/keyword-only parameters, illustrating each type with syntax, usage rules, and example code snippets, and common pitfalls such as mutable default values and argument ordering errors.
Python functions are defined using the def keyword followed by the function name and a parenthesized list of parameters. The function body starts on the next indented line.
Default (optional) arguments provide a value in the function definition using the = assignment operator. If a caller supplies a value, it overrides the default; otherwise the default is used. Multiple default arguments are allowed, but they must appear after any non‑default parameters.
Example:
def add(a, b=5, c=10):
return a + b + cThis function can be called in three ways:
print(add(3)) # Output: 18
print(add(3, 4)) # Output: 14
print(add(2, 3, 4)) # Output: 9Keyword arguments allow callers to specify parameters by name, using the syntax kwarg=value . The order of arguments is irrelevant, but each keyword must correspond to a parameter defined in the function.
print(add(b=10, c=15, a=20)) # Output: 45
print(add(a=10)) # Output: 25Positional arguments are passed based on their order in the call and must match the order of the function’s parameters. Positional arguments must appear before any keyword arguments.
print(add(10, 20, 30)) # Output: 60
print(add(10, c=30, b=20)) # Output: 60Variable‑length arguments enable functions to accept an arbitrary number of arguments. A single asterisk * collects extra positional arguments into a tuple, while a double asterisk ** collects extra keyword arguments into a dictionary.
def add(*b):
result = 0
for i in b:
result += i
return result
print(add(1, 2, 3, 4, 5)) # Output: 15
print(add(10, 20)) # Output: 30 def fn(**a):
for i in a.items():
print(i)
fn(numbers=5, colors="blue", fruits="apple")
# Output:
# ('numbers', 5)
# ('colors', 'blue')
# ('fruits', 'apple')Positional‑only and keyword‑only parameters are indicated with / and * in the parameter list. Parameters before / are positional‑only; parameters after * are keyword‑only.
def add(a, b, /, c, d):
return a + b + c + d
print(add(3, 4, 5, 6)) # Output: 18
print(add(3, 4, c=1, d=2)) # Output: 10Attempting to pass a positional‑only argument as a keyword, or a keyword‑only argument positionally, raises a TypeError .
Key takeaways include using default arguments wisely (avoiding mutable defaults), ordering arguments correctly, and choosing the appropriate parameter style to improve readability and prevent errors.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.