Syntax
def func(*args, **kwargs):Examples
Using *args
Accepting any number of positional arguments.
def sum_all(*numbers):
total = 0
for n in numbers:
total += n
return total
print(sum_all(1, 2, 3)) # 6
print(sum_all(10, 20, 30, 40)) # 100
print(sum_all()) # 0Using **kwargs
Accepting any number of named keyword arguments.
def print_profile(**details):
for key, value in details.items():
print(f"{key}: {value}")
print_profile(name="Fola", age=21, course="Software Engineering")Combining Both
A function that accepts regular parameters, extra positional args, and extra keyword args together.
def describe(title, *args, **kwargs):
print(f"Title: {title}")
print(f"Extra positional: {args}")
print(f"Extra keyword: {kwargs}")
describe("Report", "Q1", "Q2", author="Fola", pages=12)
# Title: Report
# Extra positional: ('Q1', 'Q2')
# Extra keyword: {'author': 'Fola', 'pages': 12}Unpacking When Calling
* and ** also work in reverse - spreading an existing list/dict into a function call.
def add(a, b, c):
return a + b + c
values = [1, 2, 3]
print(add(*values)) # 6
info = {"a": 10, "b": 20, "c": 30}
print(add(**info)) # 60Best practices
- Use *args when the number of positional inputs varies, and **kwargs when you want named, optional configuration options
- Place *args and **kwargs after any required named parameters in a function signature (def func(a, b, *args, **kwargs))
- Use **kwargs sparingly in your own APIs - overly flexible signatures can make it unclear what arguments are actually expected
- Combine *args/**kwargs with function wrappers or decorators when you need to pass arguments through to another function unchanged
At a glance
- Purpose
- Scripting and general-purpose applications
- File extension
- .py
- Runs in
- Python interpreter
- Usually used with
- Python standard library and packages
Specifications & further reading
Related Python documentation
def
The def keyword in Python is used to define functions - reusable blocks of code that perform specific tasks. Functions are fundamental to organizing code, avoiding repetition, and making programs more maintainable. A function can accept inputs (parameters), perform operations, and return outputs. Functions are one of the core building blocks of clean, modular Python code.lambda
A lambda is a small, anonymous, single-expression function, created without the def keyword or a name. Lambdas are restricted to a single expression whose result is automatically returned - they can't contain multiple statements or assignments. They're most useful as short, throwaway functions passed as arguments to other functions like sorted(), map(), and filter().return
The return statement exits a function immediately and optionally sends a value back to the caller. A function can return any type - including multiple values as a tuple - or nothing at all, in which case it implicitly returns None. Once return executes, no further code in the function runs, making it useful for early exits as well as producing a final result.Decorators
A decorator is a function that wraps another function to extend or modify its behavior, without changing its actual source code. Decorators use the @decorator_name syntax placed directly above a function definition, which is shorthand for passing the function into the decorator and reassigning the result. They're widely used for logging, timing, access control, caching, and validation.
The def keyword in Python is used to define functions - reusable blocks of code that perform specific tasks. Functions are fundamental to organizing code, avoiding repetition, and making programs more maintainable. A function can accept inputs (parameters), perform operations, and return outputs. Functions are one of the core building blocks of clean, modular Python code.lambda
A lambda is a small, anonymous, single-expression function, created without the def keyword or a name. Lambdas are restricted to a single expression whose result is automatically returned - they can't contain multiple statements or assignments. They're most useful as short, throwaway functions passed as arguments to other functions like sorted(), map(), and filter().return
The return statement exits a function immediately and optionally sends a value back to the caller. A function can return any type - including multiple values as a tuple - or nothing at all, in which case it implicitly returns None. Once return executes, no further code in the function runs, making it useful for early exits as well as producing a final result.Decorators
A decorator is a function that wraps another function to extend or modify its behavior, without changing its actual source code. Decorators use the @decorator_name syntax placed directly above a function definition, which is shorthand for passing the function into the decorator and reassigning the result. They're widely used for logging, timing, access control, caching, and validation.