Javaexercise.com

What are the Arbitrary Arguments in Python Functions

Python is a high-level, interpreted programming language, and is one of the most popular programming languages present in the software industry.

One of the most important concepts in python is that of arbitrary arguments used in functions. Let us have a look at it in detail.

What are arbitrary arguments in Python?

In python, arguments are used in functions to pass on specific information when they are called so that their functionality can be tailored according to requirements.

Arguments are often termed as parameters as well. One type of argument is the arbitrary argument.

These are used when you do not know the number of arguments you need to pass beforehand and work well when you need to keep the code as versatile as possible. 

Syntax

The syntax to specify arbitrary arguments in python using functions is pretty straightforward and is as follows.

When defining the function, we specify the name of the placeholder variable that will be used to access the arguments via indices.

Do keep in mind the indentations required to specify the function body.  

def <function_name>(*args):
    <statements>

If you need to access the arguments using keywords instead of indices, we use double asterix ** as follows:

def <function_name>(**kwargs):
    <statements>

To call the functions using arbitrary arguments, the syntax is as follows:

<function_name>(<argument1>, <argument2>, ….)

To call the functions using arbitrary keyword arguments, the syntax is as follows:

<function_name>(<keyword1> = <value1>,<keyword2> =  <value2>, ….)

This is a very simple syntax however there are some rules one needs to keep in mind when specifying arguments.

The order of arguments in python is very important both while defining and calling functions when using non-keyword arbitrary arguments since the indices are decided on the basis of the order of passing during the call.

When we call functions we can either use variables or values directly. 

Non-keyword arbitrary arguments in Python functions

In this example, we look at the usage of the concept of non-keyword arbitrary arguments in a function call that helps us to calculate the difference between an arbitrary number of given arguments.

One important thing here is that we need to pay special attention to the order of the arguments passed in during function calls.

Here we have a function named func that takes in an arbitrary number of arguments that are later accessed using indices using args[0] and args[1].

It stores the difference of these arguments in a variable named diff and returns it, i.e. args[0] - args[1].

We call this function first using two variables x and y, with respective argument names as well, containing values 3 and 2 respectively.

As expected the result is stored in a variable z and on printing it we see an output of 1 which is the difference between 3 and 2.

Let us take a look at the python code and corresponding output for this method:

# Function with arguments a and b
def func(*args):

    # Variable to store the difference
    diff = args[0] - args[1]

    # Returning the difference
    return diff

# Variables to store values to be passed
x = 3
y = 2

# Storing the result in variable z and printing it
z = func(x, y)

print(z)

Output

1

Now let us say we interchange the order in which the arguments are passed and pass y first followed by x, both as non-keyword arbitrary arguments.

The output gets changed and we receive -1 as the output on printing the returned value after storing it in a variable named z.

Obviously, this is not the desired output which tells us how important the order of non-keyword arbitrary arguments is while calling a function.

Let us take a look at the python code and corresponding output for this method:

# Function with arguments a and b
def func(*args):

    # Variable to store the difference
    diff = args[0] - args[1]

    # Returning the difference
    return diff

# Variables to store values to be passed
x = 3
y = 2

# Storing the result in variable z and printing it
z = func(y, x)

print(z)

Output

-1

Keyword arbitrary arguments in Python Functions

In this example, we look at the usage of the concept of keyword arbitrary arguments in a function call that helps us to calculate the difference between an arbitrary number of given arguments.

One important thing here is that we do not need to pay special attention to the order of the arguments passed in during function calls since we are using keywords.

Here we have a function named func that takes in an arbitrary number of arguments that are later accessed using keywords using args[‘a’] and args[‘b’].

It stores the difference of these arguments in a variable named diff and returns it, i.e. args[‘a’] - args[‘b’].

We call this function first using two variables x and y, with respective argument names as well, containing values 3 and 2 respectively along with keywords a and b.

As expected the result is stored in a variable z and on printing it we see an output of 1 which is the difference between 3 and 2.

Let us take a look at the python code and corresponding output for this method:

# Function with arguments a and b
def func(**kwargs):

    # Variable to store the difference
    diff = kwargs['a'] - kwargs['b']

    # Returning the difference
    return diff

# Variables to store values to be passed
x = 3
y = 2

# Storing the result in variable z and printing it
z = func(a = x, b = y)

print(z)

Output

1

Now let us say we interchange the order in which the arguments are passed and pass y first followed by x, both as keyword arbitrary arguments using keywords b and a respectively.

The output remains unchanged and we receive 1 as the output on printing the returned value after storing it in a variable named z.

This is the desired output and tells us that the order of keyword arbitrary arguments is not important while calling a function.

Let us take a look at the python code and corresponding output for this method:

# Function with arguments a and b
def func(**kwargs):

    # Variable to store the difference
    diff = kwargs['a'] - kwargs['b']

    # Returning the difference
    return diff

# Variables to store values to be passed
x = 3
y = 2

# Storing the result in variable z and printing it
z = func(b = y, a = x)

print(z)

Output

1

Conclusion

In this topic, we have learned the use and advantages of arbitrary arguments in a Python program, following some simple running examples, thus giving us an intuition of how this concept could be applied in real-world situations. Feel free to reach out to [email protected] in case of any suggestions.

 

Related Articles: