Python *args: Variable-Length Arguments Guide

Overview: *args in Python

A function normally defines a fixed number of parameters, so the function call must provide the expected number of arguments. However, some functions need to accept different numbers of positional arguments.

To solve this need, Python provides *args.

This tutorial explains what *args is, how *args works, how Python stores the arguments and how to use it in functions.

Introduction: What Is *args in Python?

*args is a special parameter that lets a function accept any number of positional arguments.

The * tells Python to collect the positional arguments, while args is the conventional name given to that parameter.

Meaning of *args

In a function definition, *args represents additional positional arguments passed to the function.

The name args is not required. Python only treats the parameter as a variable-length argument when the * is used.

def show_values(*args): 
    print(args) 

show_values(10, 20, 30) 


# Output: (10, 20, 30)

Explanation Here, *args collects the three positional arguments passed to show_values().

How *args in Python Works

When a function uses *args, Python collects the positional arguments that are not assigned to other parameters and makes them available through the args parameter.

This allows the same function to accept different numbers of positional arguments without defining a separate parameter for each value.

# How *args Stores Arguments

Python collects the positional arguments received by *args into a tuple.

This means that all the values passed through *args are stored together and can be accessed using the parameter name inside the function.

def show_values(*args):
    print(args)
    print(type(args))

show_values(10, 20, 30)
show_values(10, 20, 30, 40)


# Output:
(10, 20, 30)
<class 'tuple'>
(10, 20, 30, 40)
<class 'tuple'>

Explanation: The positional arguments are collected into the args tuple. The type() function confirms that args is a tuple.

Each function call produces a separate tuple containing the positional arguments passed in that call.

# Accessing Values in *args

Because args is a tuple, you can access its values using indexes and process its values using normal tuple operations.

def show_values(*args):
    print(args[0])
    print(args[1])

show_values(10, 20, 30)


# Output:
10
20

Explanation: Here, args[0] accesses the first value and args[1] accesses the second value.

Each function call creates a tuple containing the positional arguments received by that call.

Using *args in a Function

A function with *args can receive different numbers of positional arguments in different function calls.

Passing Multiple Positional Arguments

def show_values(*args):
    print(args)

show_values(10, 20)
show_values(10, 20, 30, 40)


# Output:
(10, 20)
(10, 20, 30, 40)

Explanation: The same function accepts two positional arguments in the first call and four positional arguments in the second call.

Looping Through *args

You can use a for loop to process each value in args one by one.

def show_values(*args):
    for value in args:
        print(value)

show_values(10, 20, 30)


# Output:
10
20
30

Explanation: The loop accesses each value in the args tuple separately.

Common Mistakes: *args in Python

Here are some common mistakes beginners make when using *args in Python:

Forgetting the *

The * is required when defining a variable-length positional parameter.

# Incorrect:
def show_values(args):
    print(args)

show_values(10, 20, 30)

# Error:
# TypeError

Explanation: Here, args is a regular parameter, so the function expects one argument instead of accepting multiple positional arguments.

#Correct:
def show_values(*args):
    print(args)

show_values(10, 20, 30)

# Output:
(10, 20, 30)

Explanation: Here, *args allows the function to receive multiple positional arguments.

↑ Move to Section Top

Treating *args as a Single Value

The values collected by *args are stored together in a tuple. Therefore, args represents the entire collection of arguments, not a single argument.

# Incorrect:
def show_values(*args):
    print(args + 10)

show_values(10, 20, 30)


# Error: 
# TypeError

Explanation Here, args contains multiple values in a tuple, so it cannot be directly added to 10.

# Correct:
def show_values(*args):
    for value in args:
        print(value)

show_values(10, 20, 30)


# Output:
10
20
30

Explanation Here, the for loop accesses each value in args separately.

↑ Move to Section Top

Confusing *args With Argument Unpacking

*args collects positional arguments in a function definition, while * can unpack an iterable when calling a function.

Passing a List as One Argument

def show_values(*args):
    print(args)

values = [10, 20, 30]
show_values(values)


# Output:
([10, 20, 30],)

Explanation: The list is passed as one positional argument, so args contains the list as a single item.

Unpacking a List Into Separate Arguments

def show_values(*args):
    print(args)

values = [10, 20, 30]
show_values(*values)


# Output:
(10, 20, 30)

Explanation: *values unpacks the list and passes its elements as separate positional arguments.

↑ Move to Section Top

Key Takeaway: *args

Here are the key points to remember about *args in Python:

  • *args in Python lets a function accept any number of positional arguments.
  • Python stores the arguments in a tuple.
  • You can access the arguments by index or process them with a for loop.
  • You can use regular parameters before *args and keyword-only parameters after it.
  • You can use *args with functions such as sum(), len(), and max().
  • In a function call, * unpacks an iterable into separate positional arguments.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top