Defining a Function with **kwargs (Arbitray Keyword Arguments)

Python function can be defined with the parameter **kwargs. So, this python function can be called with as many keyword arguments as you want.

In the arbitrary arguments(*args), we need to follow the positional arguments, whereas in the keyword arguments(**kwargs), you do not need follow the order of arguments.

In this way, kwargs are considered as a dictionary and you can access the items accordingly.
Also, you can specify your own function name and add ** before to it.

The main advantage with keyword arguments(**kwargs) is you can just specify your own parameter name and add ** before the function if you do not know number of arguments need to pass while calling the function. Example: (**emp)

Example:

def employee(**emp):
    print(“employees are:”+ emp[emp1]+” “+emp[emp2]+” “+emp[emp3])

employee(emp2=”Tom”, emp3=”Josh”,emp1=”John”)


Related Tutorials