In Python, you can define functions that accept a variable number of arguments using *args
and **kwargs
. These are incredibly useful when you’re not sure how many inputs a function will receive, or when you want to provide flexible function interfaces.
In this guide, we’ll explore how *args and **kwargs work, when to use them, and how to combine them effectively—with clear examples.
1. What are Variable-Length Arguments?
*args
: Used to pass a variable number of positional arguments.**kwargs
: Used to pass a variable number of keyword arguments (as dictionary).
2. Using *args: Positional Variable-Length Arguments
The *args
syntax allows a function to accept any number of positional arguments. These arguments are stored in a tuple.
# Example 1: Using *args
def total(*numbers):
print("Numbers received:", numbers)
print("Total:", sum(numbers))
total(5, 10, 15)
# Output:
# Numbers received: (5, 10, 15)
# Total: 30
Why use *args?
- When you don’t know how many values will be passed
- To build flexible APIs or utility functions
3. Using **kwargs: Keyword Variable-Length Arguments
The **kwargs
syntax allows you to pass a variable number of keyword arguments. These are captured as a dictionary.
# Example 2: Using **kwargs
def display_user_info(**details):
for key, value in details.items():
print(f"{key.capitalize()}: {value}")
display_user_info(name="Rahul", age=30, city="Delhi")
Why use **kwargs?
- When your function should accept optional named arguments
- To support extensible or dynamic parameters
4. Using *args and **kwargs Together
You can use both in a single function. Just remember the order: normal arguments → *args → **kwargs
.
# Example 3: *args and **kwargs together
def profile(name, *hobbies, **details):
print(f"Name: {name}")
print("Hobbies:", hobbies)
print("Details:", details)
profile("Ananya", "Reading", "Traveling", age=25, city="Mumbai")
5. Unpacking *args and **kwargs in Function Calls
You can also use * and ** when calling a function to unpack a list or dictionary into arguments.
# Example 4: Unpacking with *
def add(x, y, z):
return x + y + z
nums = [2, 4, 6]
print(add(*nums)) # Output: 12
# Example 5: Unpacking with **
def introduce(name, role):
print(f"{name} works as a {role}.")
info = {"name": "Reema", "role": "Data Analyst"}
introduce(**info)
6. Practical Use Case
# Example 6: Logging with **kwargs
def log_event(event_type, **details):
print(f"Event: {event_type}")
for key, value in details.items():
print(f"{key}: {value}")
log_event("User Login", username="gaurav", ip="192.168.1.1")
7. Summary Table
Syntax | Type | Description |
---|---|---|
*args |
Tuple | Captures variable-length positional arguments |
**kwargs |
Dictionary | Captures variable-length keyword arguments |
8. Final Thoughts
*args
and **kwargs
make your functions flexible and powerful. They’re especially useful in real-world applications like logging, configuration management, and utility toolkits. Mastering them allows you to write more scalable and generic functions without knowing all the inputs in advance.