Universal parameter transfer function parameters Detailed python

This article describes the universal parameters python function of mass participation Detailed, paper sample code described in great detail, has a certain reference value of learning for all of us to learn or work, a friend in need can refer to
us through a simple example to show how universal function parameters, let's write a simple function

def test(*args,**kwargs):
print(args,kwargs)

Then define two variables

l = [1,2,3,4]
d = {"a":1,"b":2}

Below we look at two kinds of mass participation in any different way

The first way

est(l,d)

If the transfer mode using the above parameters, the l, d 2 variables which are passed to the args parameter, as the two elements of the args variable, kwargs an empty dictionary, he does not pass any parameters a

([1, 2, 3, 4], {'a': 1, 'b': 2}) {}

We can see a list of dictionary d and l are treated as two of the elements of a tuple

The second way

test(*l,**d)

If the above way of mass participation, then l will be assigned to the variable args, d will be assigned to the variable kwargs
(1, 2, 3, 4) {'a': 1, 'b': 2}
by the above presentation, you should basically understand the universal parameters of a python, it would not know if the function uses universal parameters, how you should pass a reference to

Today was confused, again comb the universal parameters

Look at this function

def foo(action=None,**kwargs):
  print("action",action,sep="=================>")
  print("kwargs", kwargs, sep="=================>")
  
d = {"a":1,"b":2} 
foo(d)
print("=".center(100,"*"))
  
foo(**d)

Finally, I performed as follows
Here Insert Picture Description
Let me explain

The first way to call the function, passing into a dictionary, the dictionary will be passed as a whole into account, the dictionary will be assigned to the position has changed, that is, action

The second function is called method, passing through into the dictionary method **, so he actually passed a = 1, b = 2 this pass go, in fact, a variable name, and the name of these two variables are not the results of action, so the second way is to call

action to none

kwargs is a dictionary

A third method we call in view

d = {"action":"action","a":1}
foo(**d)

The following look at the results, whether you understand some of the
Here Insert Picture Description
last to recommend a very wide python learning resource gathering, [click to enter] , here are my collection before learning experience, study notes, there is a chance of business experience, and calmed down to zero on the basis of the actual project data, we can at the bottom, leave a message, do not know to put forward, we will study together progress

Published 13 original articles · won praise 8 · views 10000 +

Guess you like

Origin blog.csdn.net/haoxun10/article/details/104699508