Python with *parameters and with **parameters

One, with * parameter

1. Format: * parameter name, such as *args

2. Data type: tuple

3. Parameter transfer method: Receive any number of position parameters (parameters may not be passed).

4. Position: There can only be one in a function, and it is placed at the end (without the ** parameter).

Second, with ** formal parameters

1. Format: **formal parameter name, such as **kwargs

2. Data type: dictionary

3. Parameter passing method: Receive any keyword parameters (parameters may not be passed).

4. Position: There can only be one in a function, and it should be placed at the end.

def foo(n,*args,**kwargs):
    print("n=",n,"*args=",args,"**kwargs=",kwargs)
 
foo(10,23,45,name="tom",age=23)
n= 10 *args= (23, 45) **kwargs= {'name': 'tom', 'age': 23}


 
Three, with * actual parameters

1. Format: * actual parameter name

2. Meaning: unpacking sequences (lists, tuples, strings)

3. The way of passing parameters: it is not allowed to pass less parameters and more parameters

def foo(a,b):
    print("a=",a,"b=",b)
 
m=[6,9]
foo(*m)

Guess you like

Origin blog.csdn.net/qq_39237205/article/details/124066715