How to pass keyword parameters to a function in Python?

PythonServer Side ProgrammingProgramming

The double asterisk form of **kwargs is used to pass a keyworded, variable-length argument dictionary to a function. Again, the two asterisks (**) are the important and along with the word kwargs, indicate that there is a dictionary of variable-length keyworded arguments.

Like *args, **kwargs can take however many arguments you would like to supply to it. However, **kwargs differs from *args in that you will need to assign keywords.

Example

def print_kwargs(**kwargs):
     print(kwargs)
print_kwargs(kwargs_1="Whale", kwargs_2=5, kwargs_3= False, kwargs_4=2.1)

Output

{'kwargs_4': 2.1, 'kwargs_1': 'Whale', 'kwargs_2': 5, 'kwargs_3': False}

 

raja
Published on 07-Dec-2017 11:10:08
Advertisements