1. Introduction
Python offers flexible ways to pass data into functions. Arguments can be matched to parameters by position, by keyword name, or collected into variable-length containers using *args and **kwargs. Understanding these mechanisms lets you write functions that accept a fixed set of inputs or an open-ended, flexible set of inputs.
Cricket analogy: A captain can set a field by naming positions explicitly, like 'gully' and 'mid-on', or just by pointing fielders to spots in order, and can also leave open slots for extra fielders to fill in as substitutes come and go.
Python also allows you to restrict how arguments may be passed using positional-only markers (/) and keyword-only markers (*), which improves API clarity and prevents callers from relying on internal parameter names.
Cricket analogy: A tournament might mandate that overseas players can only be named by their registered slot number, not swapped freely mid-season, while a domestic wildcard pick must be named explicitly by the selectors, never assigned by default order.
2. Syntax
def function_name(pos_only, /, pos_or_kw, *, kw_only):
pass
def flexible(*args, **kwargs):
# args is a tuple of positional arguments
# kwargs is a dict of keyword arguments
pass
3. Explanation
Positional arguments are matched to parameters in the order they are passed. Keyword arguments are matched by name, regardless of order, using name=value syntax. A single * in the parameter list marks the start of keyword-only parameters; a single / marks the end of positional-only parameters (available since Python 3.8).
Cricket analogy: Naming a batting order 'opener, one-down, three-down' matches players to slots by their listed order, while calling out 'finisher=Dhoni' matches by name regardless of where he's listed; a single declared cutoff role like 'all specialists after this point bat only lower order' marks a boundary in the lineup.
*args collects any extra positional arguments into a tuple, and **kwargs collects any extra keyword arguments into a dictionary. This is commonly used to write wrapper functions or APIs that forward arguments to another function without knowing the exact signature in advance.
Cricket analogy: A team manager collects any extra last-minute net bowlers into one flexible practice squad without naming each individually, and separately gathers any extra logistics requests, like transport or catering, into a labeled request list — useful when forwarding needs to the ground staff without knowing the exact count in advance.
*args and **kwargs must appear in a specific order in the parameter list: standard positional parameters, then *args, then keyword-only parameters, then **kwargs. Placing them incorrectly raises a SyntaxError. Also remember *args is always a tuple and **kwargs is always a dict, even if you only pass one extra argument.
4. Example
def describe(a, b, /, c, *, d):
print(a, b, c, d)
describe(1, 2, 3, d=4)
def summarize(*args, **kwargs):
print("args:", args)
print("kwargs:", kwargs)
summarize(1, 2, 3, x=10, y=20)
5. Output
1 2 3 4
args: (1, 2, 3)
kwargs: {'x': 10, 'y': 20}
6. Key Takeaways
- Positional arguments are matched by order; keyword arguments are matched by name.
- The / marker makes preceding parameters positional-only; the * marker makes following parameters keyword-only.
- *args gathers extra positional arguments into a tuple.
- **kwargs gathers extra keyword arguments into a dictionary.
- Parameter order in a definition must be: positional, *args, keyword-only, **kwargs.
Practice what you learned
1. What data type does *args collect its values into?
2. What data type does **kwargs collect its values into?
3. In def f(a, b, /, c, *, d): what does the / do?
4. In def f(a, b, /, c, *, d): what does the * do?
5. Given describe(1, 2, 3, d=4) with def describe(a, b, /, c, *, d), what is printed?
6. What is the correct order of parameters when combining all argument types in one signature?
Was this page helpful?
You May Also Like
Functions in Python
How to define, call, and document reusable blocks of code with Python functions, including the def keyword and implicit None return.
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.
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.
Scope and Namespaces in Python
Understanding Python's LEGB scope resolution order and how the global and nonlocal keywords let functions modify variables outside their local scope.
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