Welcome to the *args and **kwargs Tutorial Module. This guide comprehensively explains variable positional arguments, variable keyword arguments, dictionary unpacking, parameter ordering rules, and version evolution across Python releases.
In Python:
*args(Arbitrary Positional Arguments): Allows a function to accept any number of positional arguments. Inside the function,argsis received as atuple.**kwargs(Arbitrary Keyword Arguments): Allows a function to accept any number of keyword (named) arguments. Inside the function,kwargsis received as adict.
The names
argsandkwargsare conventions; the asterisks (*and**) are what actually perform the unpacking / packing.
When defining functions that take multiple types of arguments, Python enforces a strict declaration order:
def example(required_pos, *args, keyword_only="default", **kwargs):
passYou can also unpack iterables into positional arguments and dictionaries into keyword arguments:
numbers = [10, 20, 30]
average_score(*numbers) # Unpacks list into positional arguments
details = {"brand": "Jack Daniels", "in_stock": True}
drinks_type("Whisky", 25.99, **details) # Unpacks dictionary into kwargs| File | Description | Core Concept |
|---|---|---|
ava_score.py |
Arithmetic mean calculation for variable inputs | *args numerical aggregation |
clients_plus_args.py |
Required parameters combined with extra tags | Fixed parameters + *args |
customer_kwargs.py |
Flexible customer metadata logging | Combined *args and **kwargs |
drink_type.py |
Mandatory parameters + arbitrary attributes | Fixed parameters + **kwargs |
alcohols.py |
Categorizing item names and properties | Flexible packing and iteration |
students.py |
Clean parameter naming and default arguments | Readability & keyword arguments |
test_kwargs.py |
Automated unit test suite (unittest) |
Comprehensive test verification |
Because args is a tuple and kwargs is a dict, you can inspect their available methods using dir():
def inspect_types(*args, **kwargs):
print("Methods available on args (tuple):")
print([m for m in dir(args) if not m.startswith("__")])
# Output: ['count', 'index']
print("Methods available on kwargs (dict):")
print([m for m in dir(kwargs) if not m.startswith("__")])
# Output: ['clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']| Feature | Introduced / Changed In | Description |
|---|---|---|
| Python 2.7 | Legacy | *args and **kwargs supported, but keyword-only parameters were not available without manual dictionary popping. |
| Python 3.0+ | PEP 3102 | Added Keyword-Only arguments following a bare * or *args. |
| Python 3.5 | PEP 448 | Additional Unpacking Generalizations — allow multiple *args and **kwargs in function calls: func(*list1, *list2, **dict1, **dict2). |
| Python 3.8 | PEP 570 | Added Positional-Only arguments syntax (/): def func(pos_only, /, standard, *, kw_only): .... |
| Python 3.12+ | Performance Tuning | Comprehensions and unpacking operations inlined at bytecode level for faster function calls and lower memory overhead. |
Execute the test suite from the repository root:
python3 -m unittest kwargs/test_kwargs.pyExpected output:
......
----------------------------------------------------------------------
Ran 6 tests in 0.001s
OK