Many of us might be aware of these args
and kwargs
variables, but for those out there who don't know about these variables, here is a post dedicated for you. The *args
and **kwargs
are conventions to represent two variables, which are mostly used in defining functions in Python. Both these variables are used to pass a varying number of parameters to any method. This means the number of parameters needn't be known beforehand. The operator *
indicates that varying numbers of parameters can be passed to the method. When a variable gets associated with the *
, it is considered as an iterable in python and it can be iterated over. It is used with methods that are not keywords, and have a variable-length argument list.
Demonstration of *args
with an example
def demonstrate_args(arg_1, *argv):
print("Argument one-", arg_1)
for arg in argv:
print("Other arguments-", arg)
demonstrate_args('Hello', 'We', 'are', 'Studytonight')
Output:
Argument one- Hello
Other arguments- We
Other arguments- are
Other arguments- Studytonight
What is **kwargs
?
It helps pass a keyworded variable length of arguments to a function. It is used when named arguments need to be handled in a method. Below is an example demonstrating the same:
Time for an example:
def demonstrate_kwargs(**kwargs):
for key, value in kwargs.items():
print("{0} = {1}".format(key, value))
demonstrate_kwargs(a = 'Hi', b = 'there', c = 'Studytonight')
Output:
a = Hi
b = there
c = Studytonight
Using *args
and **kwargs
in Python functions
In the below example, we have used *args
and **kwargs
to call a function which contains a list or dictonary of arguments.
Normal way of passing every argument to a function:
Normally if a function expects 3 arguments, while calling that function we should provide 3 argument values separated by comma. The number of comma separated values provided at the time of function calling must be equal to the number of arguments required by that function.
def normal_args(arg_1, arg_2, arg_3):
print("Argument 1:", arg_1)
print("Argument 2:", arg_2)
print("Argument 3:", arg_3)
normal_args(1,2,3)
Output:
Argument 1: 1
Argument 2: 2
Argument 3: 3
Using *args
while passing arguments
We can use the *args
variable to store multiple argument values to be provided to a function in a single variable as shown in the code example below.
def star_args(arg_1, arg_2, arg_3):
print("Argument 1:", arg_1)
print("Argument 2:", arg_2)
print("Argument 3:", arg_3)
args = (1,2,'Studytonight')
star_args(*args)
Output:
Argument 1: 1
Argument 2: 2
Argument 3: Studytonight
Using **kwargs
to pass arguments
If we wish to provide argument values in form of key value pairs, where for each value provided a key is added to the value then we can use the **kwargs
variable to store the key-value pairs.
def star_k_args(arg_1, arg_2, arg_3):
print("Argument 1:", arg_1)
print("Argument 2:", arg_2)
print("Argument 3:", arg_3)
kwargs = {"arg_2": 2, "arg_1": "one", "arg_3": 'Studytonight'}
star_k_args(**kwargs)
Output:
Argument 1: one
Argument 2: 2
Argument 3: Studytonight
Note: While using *args
and **kwargs
in a single function, the *args
is followed by **kwargs
in the function arguments.
Conclusion:
In this post, we saw how the args and kwargs variables are used in functions. Based on our requirements, we use the args and kwargs, mostly using them when working on python function decorators.