Look at python example code below:
def get(*args, **kwargs): pass
In this code, we have used *args and **kwargs in python function. What are their meaning? As to *args, we have known it is a python tuple. Here is the tutorial:
Understand Python *args Parameter: A Beginner Guide
How about **kwargs parameter in python function?
We will introduce it using some examples in this tutorial.
Run python example code below:
def get(*args, **kwargs): print(type(kwargs)) get()
We can find: **kwargs is a python dict.
<class 'dict'>
How to get the value in **kwargs?
We can use **kwargs as a python dictionary.
Here is an example:
def get(*args, **kwargs): print(args) print(type(kwargs)) for k, v in kwargs.items(): print(k,"=",v) get(1, 2, 3, name='Tom', age=30)
Run this code, we will get these values:
(1, 2, 3) <class 'dict'> name = Tom age = 30
From this result, we can find:
**kwargs is a python dictionary, we can use key=value to pass parameters to it.
However, we can not do as follows:
get(1, 2, 3, 'name':'Tom', 'age':30)
We also can not do like this:
get(1, 2, 3, {'name':'Tom', 'age':30})
Run this code, you will find {‘name’:’Tom’, ‘age’:30} is saved in *args.
{'name':'Tom', 'age':30}