Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

How to pass keyword parameters to a function in Python?

If I use the following code

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

I am able to pass keyword parameters to a python function. How is this possible?


1 Answer
Rajendra Dharmkar

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.

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}

 

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.