** in python

  • In Python ** is an exponential operator.The double asterisk form of **kwargs is used to pass a keyword, variable-length argument dictionary to a function. Again, the two asterisks (**) are the important element here, as the word kwargs is conventionally used, though not enforced by the language.
  • Like *args, **kwargs get however several arguments you would like to supply into it. Though **kwargs differs from *args in that you will need to assign keywords.

First, let’s simply print out the **kwargs arguments that we pass to a function. We’ll create a short function to do this:

print_kwargs.py

def print_kwargs(**kwargs):
print(kwargs)

Next, we will call the function with some other keyword arguments passed into the function:

Sample Code:

def print_kwargs(**kwargs):
print(kwargs)
print_kwargs(kwargs_1="Shark", kwargs_2=4.5, kwargs_3=True)

Output :

{'kwargs_3': True, 'kwargs_2': 4.5, 'kwargs_1': 'Shark'}

Categorized in:

Tagged in:

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,