Variable-Length Arguments

Carson West

Function Overloading

Variable-Length Arguments in Python

Python offers flexibility in Function Definitions by allowing variable-length arguments. This means you can define functions that accept a varying number of arguments, without needing to specify the exact number beforehand. There are two main ways to achieve this:

def my_sum(*args):
  total = 0
  for num in args:
    total += num
  return total

print(my_sum(1, 2, 3))  # Output: 6
print(my_sum(10, 20, 30, 40)) # Output: 100
print(my_sum()) # Output: 0
def print_details(**kwargs):
  for key, value in kwargs.items():
    print(f"{key}: {value}")

print_details(name="Alice", age=30, city="[New York](./../new-york/)")

Output:

name: Alice
age: 30
city: [New York](./../new-york/) ```

* **Combining `*args` and `**kwargs`:**

  You can combine both `*args` and `**kwargs` in a single function definition.  The order matters: `*args` must come before `**kwargs`.

```python
def combined_example(*args, **kwargs):
    print("Positional arguments:", args)
    print("Keyword arguments:", kwargs)

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

Output:

Positional arguments: (1, 2, 3)
Keyword arguments: {'name': 'Bob', 'age': 25}

Tuple Unpacking Dictionary Manipulation

These are essential for understanding how to fully utilize *args and **kwargs.