Meaning of *args and **kwargs in Python function definition
Understanding *args and **kwargs, and how to use them in your function definitions
In programming, functions are fragments of codes that perform a specific task which when put together make a program. Although, in Object Oriented Programming (OOP) languages like Python, functions are referred to as methods. Methods in OOP terms, are functionalities of a class that can be used by any object of that class.
For a better understanding of the meaning and use of *args and **kwargs, you need to have a basic knowledge of the OOP terms class, object, inheritance, and methods. If not, you can read about it here before you continue with this article.
What is *args and how to use it?
In layman's language, *args is just a placeholder that allows us to pass an unspecified number of parameters to a function. *args is used in a function definition to pass a variable number of non-keyword arguments when calling the function and storing them as a tuple.
The syntax for defining *args in a function
def func(*args):
for arg in args:
print(arg)
# calling the function and passing three arguments to it
func(1, 2, 3)
—--------------------------------------------------------------------------
# This is what our output will look like
# 1
# 2
# 3
What is **kwargs and how to use it?
Again, **kwargs is a placeholder that allows us to pass an unspecified number of parameters to a function. But unlike *args, **\kwargs is used to pass a variable number of keyword arguments and store them as a dictionary.
The syntax for defining **kwargs in a function
def func_kwargs(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")
# calling the function and passing keyword arguments
func_kwargs(A=1, B=2, C=3)
-------------------------------------------------------------------------
# Our output will look like something like this
# A: 1
# B: 2
# C: 3
How to use *args and \*kwargs in one function*
Note that it is possible to use *args and **kwargs in one function definition. Below is the syntax for using *args and **\kwargs together in a function:
def func_args_kwargs(*args, **kwargs):
for arg in args:
print(f”Non keyword example: {arg}”)
for key, value in kwargs.items():
print(f”Keyword example: {key}={value}”)
#calling our function
func_args_kwargs(1, 2, 3, a=4, b=5, c=6)
—-------------------------------------------------------------------------
# Non keyword example: 1
# Non keyword example: 2
# Non keyword example: 3
# Keyword example: a=4
# Keyword example: b=5
# Keyword example: c=6
Note that the syntax for using args and **kwargs in a function definition matters a lot. If you intend to use normal arguments too then the normal argument comes first. Thus the syntax will be as follow:
def all_args(normal_args, *args, **kwargs):
pass
Any other syntax different from the one above will return an error message.
In conclusion, *args and **\kwargs help us to pass a variable number of arguments to our function. This is very helpful especially when we don’t know the exact number of arguments our function will be taking beforehand.