Sitemap
Namira Ra'ufa Dayyana

Namira Ra'ufa Dayyana

Function with Variable-Length Arguments in Python

2 min readSep 8, 2024

--

Introduction

In Python, functions are useful tools that help you group code together and use it multiple times. One of the advanced feature of Python functions is that they can take different numbers of arguments, which makes them very flexible. This article will explain what variable-length arguments are in Python, how to use them, and provide some practical examples.

Understanding Functions in Python

A function in Python is a block of reusable code designed to perform a single, related action. Functions help in organizing code, making it more readable, and reducing redundancy. Here is a simple example of a function:

def greet(name):
return f"Hello, {name}!"
print(greet("Namira"))
# Output: Hello, Namira

Variable-Length Arguments

Variable-length arguments in Python allow you to pass a variable number of arguments to a function. This is useful when you do not know beforehand how many arguments might be passed.

Using *args for Positional Arguments

The *args syntax is used to pass a variable number of non-keyword arguments to a function. Inside the function, *args is a tuple of arguments.

def sum_all(*args):
return sum(args)
print(sum_all(1, 2, 3, 4))
# Output: 10

Using **kwargs for Keyword Arguments

The **kwargs syntax allows you to pass a variable number of keyword arguments to a function. Inside the function, **kwargs is a dictionary of arguments.

def greet_all(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")
greet_all(name="Alice", age=25, city="New York")

Combining *args and **kwargs

You can use both *args and **kwargs in a single function to accept any number of positional and keyword arguments.

def display(*args, **kwargs):
for arg in args:
print(arg)
for key, value in kwargs.items():
print(f"{key}: {value}")
display(1, 2, 3, name="Alice", age=25)

Example of *args and **kwargs: Practical Use Case

Here’s a practical example where both *args and **kwargs are used in a function that processes order details:

def process_order(order_id, *items, **customer_details):
print(f"Order ID: {order_id}")
print("Items:")
for item in items:
print(f" - {item}")
print("Customer Details:")
for key, value in customer_details.items():
print(f"{key}: {value}")
process_order(123, "apple", "banana", "cherry", name="John Doe", address="123 Elm St", phone="123–456–7890")

Differences with Manual Function

Creating functions manually with predefined arguments limits flexibility and can lead to code duplication. For example, a manual function handling a fixed number of arguments looks like this:

def process_order_manual(order_id, item1, item2, item3, name, address, phone):
print(f"Order ID: {order_id}")
print("Items:")
print(f" - {item1}")
print(f" - {item2}")
print(f" - {item3}")
print("Customer Details:")
print(f"name: {name}")
print(f"address: {address}")
print(f"phone: {phone}")
process_order_manual(123, "apple", "banana", "cherry", "John Doe", "123 Elm St", "123–456–7890")

Using *args and **kwargs allows for more dynamic and reusable code, reducing redundancy and improving maintainability.

Conclusion

Variable-length arguments using *args and **kwargs enhance the flexibility and functionality of functions in Python. They allow you to write more generic, reusable, and concise code, adapting to various input scenarios effortlessly.

Get Namira Ra'ufa Dayyana’s stories in your inbox

Join Medium for free to get updates from this writer.

— -

--

--