Arbitrary Arguments

basic
Published

January 4, 2025

*args: Arbitrary Positional Arguments

The *args syntax allows a function to accept any number of positional arguments. These arguments are collected into a tuple named args (you can choose a different name, but args is the convention). This is incredibly useful when you don’t know beforehand how many arguments a function might need.

def my_sum(*args):
  """Calculates the sum of all input numbers."""
  total = 0
  for number in args:
    total += number
  return total

print(my_sum(1, 2, 3))  # Output: 6
print(my_sum(10, 20, 30, 40, 50))  # Output: 150
print(my_sum()) # Output: 0

As you can see, my_sum can handle any number of arguments, making it highly versatile.

**kwargs: Arbitrary Keyword Arguments

Similar to *args, **kwargs allows a function to accept any number of keyword arguments. These arguments are collected into a dictionary named kwargs (again, the name is conventional). This is particularly helpful when you want to provide optional settings or configurations to a function.

def print_details(**kwargs):
  """Prints the key-value pairs from keyword arguments."""
  for key, value in kwargs.items():
    print(f"{key}: {value}")

print_details(name="Alice", age=30, city="New York")

print_details(country="USA", profession="Engineer")

In this example, print_details accepts and prints an arbitrary number of key-value pairs.

Combining *args and **kwargs

You can even combine *args and **kwargs in a single function definition to handle both positional and keyword arguments flexibly:

def versatile_function(*args, **kwargs):
  """Demonstrates the use of both *args and **kwargs."""
  print("Positional arguments:", args)
  print("Keyword arguments:", kwargs)

versatile_function(1, 2, 3, name="Bob", age=25)

This function showcases the ultimate flexibility of handling a completely variable number of inputs. Remember the order: *args must come before **kwargs in the function definition.

Practical Applications

*args and **kwargs are essential in various scenarios:

  • Creating flexible functions: Design functions that can handle different input requirements without needing multiple overloaded function versions.
  • Extending functionality: Easily incorporate additional parameters without modifying the core function’s signature.
  • Working with libraries and APIs: Many libraries use these features to pass variable sets of parameters.

Using *args and **kwargs effectively makes your Python code cleaner, more maintainable, and highly adaptable to various input conditions.