We can pass dynamic parameters to the function, *args,**kwargs, first let's look at *args, the example is as follows:
(*args)
def show(*args): print(args,type(args)) # Passing parameters to a list as a tuple show(11,22,33,44,55,66)
First we defined a function, function show (* args) inside the * args can receive dynamic parameters, here we receive a tuple form of parameters, we can show () inside the pass a lot of parameters, the function defaults to these parameters as a tuple to receive.
(**args)
def show(**kwargs): print(kwargs,type(kwargs)) # Passing arguments to a function as a dictionary show(k1=80,k2="alex")
As can be seen from the code above, **kwargs receives parameters in the form of a dictionary, and we know that a dictionary is composed of key-value pairs, key-value, and thus we must pass parameters in the form of key-value pairs into it to be able to receive them, and thus we pass the parameters k1=80,k2="alex" in it. This generates a dictionary, as you can see from the code.
(k,*args)
def show(k,*args):
print(k,type(k))
print(args,type(args))
show([11,22],33,44,55,88)
The results of the run are as follows:
[11, 22] <class 'list'> (33, 44, 55, 88) <class 'tuple'>
As can be seen from the above code, we define two parameters to the function, one is k, one is *args, we know that the formal parameter k can only receive an object parameter, *args can receive more than one parameter and put in a tuple, the following we passed more than one parameter to the function to see how it is received. From the results of the run (1) can be seen, the formal parameter k received parameters [11,22]; and the back of the value of the input passed to the *args, and stored in a list.
(*args,**kwargs)
As long as the function has these two parameters then, you can pass any form of real parameter in, because you can receive any form of parameter, but we know that the dictionary must be composed of key-value pairs, and the parameters passed should satisfy the key=value format, or else it will report an error. This must be remembered, the default method of passing parameters to a dictionary in Python is key-value pairs.
def show(*args,**kwargs): print(args,type(args)) print(kwargs,type(kwargs)) show(123,"alex",666,alex="sb",nanyang="degnzhou")
The results of the run are as follows:
(123, 'alex', 666) <class 'tuple'> {'nanyang': 'degnzhou', 'alex': 'sb'} <class 'dict'>
We can see that the front elements are put into a tuple tuple, the back elements are put into a dictionary dict, but we must remember, *args, must be in front of **kwargs, otherwise it will report an error, the order must be a single parameter, dynamic tuple, dynamic dictionary way.
Let's look at an example below:
def show(*args,**kwargs): print(args,type(args)) print(kwargs,type(kwargs)) l = [11,22,33,44] d = {"n1":88,"alex":"sb"} #We want to pass the list l to the formal parameter *args, and the dictionary to the formal parameter **kwargs, and see if the following is possible show(l,d) (1)
# If you want to realize the above function, you have to represent it and specify which parameter to pass to the formal parameter *args, and the other parameter to the formal parameter ××kwargs.
show(*l,**d) (2)
In the above code, we call the parameter directly at (1), but the result we get is not what we want, we want to pass the parameter in the form of a list or dictionary to the corresponding formal parameter, then it must be labeled as to which parameter is passed, equivalent to the default parameter as well. Otherwise, it will only be passed to the first parameter *args, which puts both the list and the dictionary in the same tuple. The result of the above code is as follows:
Run results:
([11, 22, 33, 44], {'alex': 'sb', 'n1': 88}) <class 'tuple'> {} <class 'dict'> (11, 22, 33, 44) <class 'tuple'> {'alex': 'sb', 'n1': 88} <class 'dict'>
5. The case of parameters in strings
(1) String formatting, passing list parameters to the string
s1 = "{0} is {1}." result = ("alex","sb")(1) l = ["alex","sb"] (2) res = (*l) print(result) print(res)
Above, we have two methods of passing arguments to a string, in method (1) we pass arguments directly to s1 in order and in method (2) we specify a list but use *args to pass arguments to the string s1. The list is ordered.
(2) Passing parameters in dictionary form to a string
s1 = "{name} is {acter}." result = (name="alex",acter="sb") d = {"name":"alex","acter":"sb"} # Pass dictionary-form parameters to the list res = (**d) print(result) print(res)
In the above code, we are passing a parameter in the form of a dictionary to the string, and we know the format of a dictionary, and we have to specify that the parameter we are passing is a dictionary when we pass it to the string.
Thus to use the **d format, tell Python.
displayed formula
A lambda simple expression is a simple representation of a function:
>>> func = lambda a:a+1 >>> ret = func(99) >>> print(ret) 100
In the above form, func is defining a function, and lambda means that we are defining a lambad expression, where a represents the formal parameter, and a+1 after the colon (:) represents the expression, to the
What kind of processing the function performs, and then returns the result to the variable ret, the variable that called it. Omit return.
There can be more than one formal parameter above; before the colon are the parameters, which can be more than one; after the colon is the function body.
The lambda expression creates the formal parameter a; the function contents a+1 and returns the result to the variable that called the function.
Above this talk function (function) in the dynamic parameters is all I have shared with you, I hope to give you a reference, and I hope you support me more.