python* and **

1. When calling a function:

args=(1,2,3)

test(*args)

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

kwargs={'a':1,'b':2,'c':3}

test(**kwargs)

The function is to pass the dictionary kwargs into keyword arguments. 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).

2. When defining function parameters:

def test(*args):

Here *args means that all incoming positional parameters are installed in the tuple args. For example, if the above function calls test(1, 2, 3), the value of args is (1, 2, 3). :

def test(**kwargs):

** is for keyword arguments and dictionaries. When calling test(a=1,b=2,c=3), the value of kwargs is {'a':1,'b':2,'c':3}.

-------------------------------------------------------------------------------------------------------

**Two multiplication signs are the power, such as 2**4, the result is 2 to the 4th power, the result is 16
A multiplication sign *, if the operand is two numbers, it is the multiplication of these two numbers, such as 2*4, the result is 8
*If a string, list, tuple is multiplied by an integer N, return an object of the same type with all elements repeated N times, such as "str" ​​*3 will return the string "strstrstr"

--------------------------------------------------------------------------------------------------------

定义:test(x,y,z)

调用:test(1,2,3)或test(*(1,2,3))

定义:test(*args)

调用:test(1,2,3)


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326550971&siteId=291194637