Parameters of functions in Python (variable parameters and keyword parameters)

Function parameters (variable parameters and keyword parameters)
in Python are used when passing parameters to functions and function definitions. You will often see and * and **. The functions are explained below.
Use * and ** when calling functions,
assuming there is a function
def test(a, b, c)

test(*args): The function of * is actually to pass each element in the sequence args as a positional parameter. For example, in the above code, if args is equal to (1,2,3), then this code is equivalent to test(1, 2, 3).

test(**kwargs): The function of ** is to transfer dictionary kwargs into keyword parameters. For example, in the above code, if kwargs is equal to {'a':1,'b':2,'c':3}, then this code is equivalent to test(a=1,b=2,c=3).

Use * and ** when defining function parameters

def test(*args):
  The meaning of * when defining function parameters is different. Here *args means that all the passed positional parameters are packed in the tuple args. For example, in the above function, if test(1, 2, 3) is called, the value of args is (1, 2, 3). :

def test(**kwargs):
  similarly, ** is for keyword parameters and dictionaries. If you call test(a=1,b=2,c=3), the value of kwargs is {'a':1,'b':2,'c':3}.

Guess you like

Origin blog.csdn.net/weixin_46327703/article/details/108444883