Discussion on dynamic parameters in function of function

  • 2020-05-30 20:30:43
  • OfStack

We can pass dynamic parameters to the function,* args,**kwargs, first let's look at *args, the example is as follows:

1.show(*args)


def show(*args):
print(args,type(args))
# Pass arguments to the list as tuples 
show(11,22,33,44,55,66)

First of all, we defined a function, the function show(*args) *args can receive dynamic parameters, here we receive 1 tuple of parameters, we can pass many parameters to show(), the function default to receive these parameters as 1 tuple.

2.show(**args)


def show(**kwargs):
print(kwargs,type(kwargs))
# Pass arguments to a function in the form of a dictionary 
show(k1=80,k2="alex")

Can be seen from the code above, * * kwargs is receiving parameters in the form of a dictionary, we know that the dictionary is composed of key-value pairs, key - value, therefore we must to reach out to key - value of the parameters of the form to receive, so we in the incoming parameters k1 = 80, k2 = "alex". This generates a dictionary, could be judged from the code.

3.show(k,*args)

def show(k,*args):
print(k,type(k))
print(args,type(args))
show([11,22],33,44,55,88)

The results are as follows:


[11, 22] <class 'list'>

(33, 44, 55, 88) <class 'tuple'>

As can be seen from the above code, we defined two parameters to the function, one is k, one is *args, we know that the formal parameter k can only receive one object parameter, *args can receive multiple parameters and put them in a tuple, we passed multiple parameters to the function below, see how to receive. As can be seen from the operation result (1), k receives parameters [11,22]. The values entered later are passed to *args and stored in a list.

4.show(*args,**kwargs)

As long as the function has these two parameters, then you can pass in any type of argument, you can kill people, you can kill buddhas, because you can take any type of argument, but we know that the dictionary has to be made up of key-value pairs, you have to pass in key=value, otherwise you get an error. Remember that the default method in Python to pass parameters to a dictionary is a key-value pair.


def show(*args,**kwargs):
print(args,type(args))
print(kwargs,type(kwargs))

show(123,"alex",666,alex="sb",nanyang="degnzhou")

The results are as follows:


(123, 'alex', 666) <class 'tuple'>

{'nanyang': 'degnzhou', 'alex': 'sb'} <class 'dict'>

As we can see, the first element is put into a tuple tuple, and the second element is put into a dictionary dict, but we must remember that *args, 1 must be in front of **kwargs, otherwise there will be an error. Order 1 must be a single parameter, dynamic tuple, dynamic dictionary mode.

Here's an example:


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 make a list l Pass to the formal parameter *args, Pass the dictionary to the parameter **kwargs , see if the following way is ok 
show(l,d)  ( 1 ) 

To do this, specify which parameter to pass to parameter *args and the other parameter to pass to parameter ××kwargs

l show (*, * * d) (2)

In the above code, we directly call the parameter at (1), but the result is not what we want. We want to pass the parameter to the corresponding parameter in the form of a list or dictionary, so we must indicate which parameter is passed, which is equivalent to the default parameter 1. Otherwise, only the first argument *args is passed, placing both the list and the dictionary in the same tuple. The result of the above code is as follows:

Operation 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. Parameters in a string

(1) string formatting, passing list parameters to the string


s1 = "{0} is {1}."
result = s1.format("alex","sb") ( 1 ) 
l = ["alex","sb"]  ( 2 ) 
res = s1.format(*l)
print(result)
print(res)

Above, we have two ways of passing arguments to a string. In method (1) we pass the arguments directly to s1 in order, and in method (2) we specify a list, but pass the arguments to the string s1 in the form of *args. The list is ordered.

(2) pass parameters in dictionary form to the string


s1 = "{name} is {acter}."
result = s1.format(name="alex",acter="sb")
d = {"name":"alex","acter":"sb"}
# Pass a dictionary-style parameter to the list 
res = s1.format(**d)
print(result)
print(res)

In the above code, we passed a dictionary parameter to the string. We know the format of the dictionary, and when we pass it to the string, we specify that the parameter we pass is 1 dictionary,

So to use the **d format, tell Python.

6. lambda expression

lambda simple expression is the simple representation of the function:


>>> func = lambda a:a+1
  >>> ret = func(99)
  >>> print(ret)
  100

In the above form, func is the definition of 1 function, and lambda is the definition of 1 lambad expression, where a is the parameter, and a+1 after the colon (:) is the expression, right

What does the function do, and then returns the result to the variable ret and calls its variable. return is omitted.

The above formal parameters can have more than one, before the colon is a parameter, can have more than one; The colon is followed by the body of the function.

The lambda expression creates the parameter a; Function contents a+1 and returns the result to the variable that called the function.


Related articles: