PythonFunctionsIntermediate

*args and **kwargs

Accepts variable positional and keyword arguments in a function.

Review the syntaxStudy the examplesOpen the coding app
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

*args (optional)tuple · Extra positional args captured as a tuple.
**kwargs (optional)dict · Extra keyword args captured as a dict.

See it in practice

Examples

1

Basic Positional Packing

def multiply_all(*args):
    result = 1
    for num in args:
        result *= num
    return result

print(multiply_all(2, 3, 4))
Output:
24

The *args parameter collects all positional arguments into a tuple named args.

2

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'))
Output:
{'user': 'Alice', 'age': 30, 'city': 'NY'}

The **kwargs parameter captures named arguments into a dictionary.

3

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'))
Output:
Executing function... Hello World

Standard pattern for decorators to pass all arguments to the wrapped function.

Debug faster

Common Errors

1

SyntaxError

Cause: Defining **kwargs before *args in the function signature.

Fix: Always place *args before **kwargs.

def my_func(**kwargs, *args): # Invalid syntax
    pass
2

TypeError

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

Python 3.8+

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.