*args and **kwargs
Accepts variable positional and keyword arguments in a function.
def f(*args, **kwargs):
...This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Accepts variable positional and keyword arguments in a function.
In Python, *args and **kwargs allow functions to handle a variable number of arguments, providing significant flexibility for API design and function wrapping. The *args syntax packs surplus positional arguments into an immutable tuple, while **kwargs packs named arguments into a mutable dictionary. Performance-wise, using these operators involves a small overhead for tuple and dictionary creation during the function call. A critical edge case is the ordering: in a function signature, formal positional arguments must come first, followed by *args, then keyword-only arguments, and finally **kwargs. Since Python 3.x, any parameters defined after *args are strictly keyword-only. These tools are essential for implementing decorators and proxy patterns where a function must forward all received inputs to another callable without knowing the specific signature beforehand.
Quick reference
Syntax
def f(*args, **kwargs):
...
Inputs
Parameters
See it in practice
Examples
Basic Positional Packing
def multiply_all(*args):
result = 1
for num in args:
result *= num
return result
print(multiply_all(2, 3, 4))24
The *args parameter collects all positional arguments into a tuple named args.
Keyword Packing for Attributes
def build_profile(name, **info):
profile = {'user': name}
profile.update(info)
return profile
print(build_profile('Alice', age=30, city='NY')){'user': 'Alice', 'age': 30, 'city': 'NY'}
The **kwargs parameter captures named arguments into a dictionary.
Function Argument Forwarding
def decorator_func(func):
def wrapper(*args, **kwargs):
print('Executing function...')
return func(*args, **kwargs)
return wrapper
@decorator_func
def say_hello(name):
return f'Hello {name}'
print(say_hello('World'))Executing function... Hello World
Standard pattern for decorators to pass all arguments to the wrapped function.
Debug faster
Common Errors
SyntaxError
Cause: Defining **kwargs before *args in the function signature.
Fix: Always place *args before **kwargs.
def my_func(**kwargs, *args): # Invalid syntax
passTypeError
Cause: Passing a positional argument that matches a parameter already captured by a positional assignment.
Fix: Ensure that specific positional arguments are not duplicated in the keyword arguments call.
def greet(name, **kwargs): pass
greet('Bob', name='Alice') # Multiple values for 'name'Runtime support
Compatibility
Source: MDN Web Docs
Common questions
Frequently Asked Questions
Accepts variable positional and keyword arguments in a function.
*args: Extra positional args captured as a tuple. **kwargs: Extra keyword args captured as a dict.
SyntaxError: Always place *args before **kwargs. TypeError: Ensure that specific positional arguments are not duplicated in the keyword arguments call.