* And ** What does it mean in Python? And address it with the pointer on the right?

* And ** have many uses:

  • (*) Is the multiplication operator (or in the case of the string is repeated operator). Other libraries in the class for other reasons may use '*', but almost always multiply in some form.
  • (**) is an exponential operator, is calculated mathematically x ** yXÿ in normal numbers
    but these are not surprising usage - every developer may know usage; but I think the problem is almost certainly with but the most common undesirable local Governments (at least for beginners):

Variable parameter of the function

** and * operators appear in the function definition - for example:

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

To understand their role, you need to know the location and keyword arguments.

Keyword arguments function is most easily explained, because any parameters passed to the function of the form are key parameters. Next, the positional parameters are all the parameters of the function call, rather than keyword parameters. <Argument_name> =

Back to our function definition (as described above) is to collect all the positional parameters into a single tuple (to be called) in the instruction, and is collected together all the key parameters into a single dictionary named in the instructions. Common names and conventions - you can call them anything you want. * Argsargs ** kwargskwargsargskwargs

An example of calling this function is:

>>> FUNC (1 ,2 ,3 ,一个= 4 ,b = 5 ,C ^ = 6 )
(1 ,2 ,3 ){ 'A' :4 ,'B' :5 ,'C' :6 }  		

As you can see, the parameters of 1, 2 and 3 are collected into the args tuple is called, and keyword parameters a, b and c are collected to the dictionary named in kwargs.

You can even mix and normal position and keyword arguments together: * args ** kwargs

def func2 (a ,b ,* args ,c = None ,d = None ,** kwargs ):  
	print(a ,b ,args )
	print ('c =' ,c ,'d =' ,d ,kwargs )  

Here the first two parameters are passed to the positions a & b- position and any remaining parameters are packed into the tuple args.
Further, c & dkeywords is normally captured parameters (if not provided, the default value), and any other key parameters are packed into kwargs dictionary.

E.g:

>>> FUNC2 (1 ,2 ,3 ,4 ,Ç = 6 ,d = 7 ,ë = 8 ,˚F = 9 )   
1 2 (3 ,4 )  	 
c = 6   d = 7 { 'e' :8 ,'f' :9 }

In all cases args and kwargs (or whatever you call it a) can be empty - for example - using the above func2 function:

>>> FUNC2 (1 ,2 ,Ç = 6 ,d = 7 ) 
1 2 ()   
c = 6 d = 7 {}  	

In this call, only two position parameters, which are captured in the parameters in a & b, so the no args can be packaged into tuples. Similarly, only two key parameters (c & d), and therefore no additional keywords parameters can be packaged into kwargs dictionary.

Out of the box

As you can use * and ** in the function definition operator function parameters packaged tuple or a dictionary (as described above), you can use them to tuple or unpacked dictionary function parameters. For example, using this feature:

def func3 (a ,b ):
	print(a ,b )

This function takes two parameters location - if you already have these two parameters in a tuple, you can do one of two things:

>>> T0 = (0 ,1 ) 
>>> func3 (t0 [ 0 ],t0 [ 1 ])
0 1	
>>> func3 (* t0 ) 
0 1	 

Here the operator to decompress tuple for the location parameter. Although both of the functionally equivalent, but using operator method is about 60% of another version of the execution time, and more readable (the error is less likely).

Similarly, the dictionary is possible to extract as keyword arguments - Use this function takes keyword arguments:

DEF FUNC4 (KW1 = 0 ,KW2 = 1 ): 
	print(KW1 ,KW2 }

If you have a dictionary that contains the desired value, then there are two options:

>>> D0 = { 'KW1' :10 ,'KW2' :11 }  
>>> FUNC4 (KW1 = D0 [ 'KW1' ],KW2 = D0 [ 'KW2' ])
10 11 	
>>> func4 (** d0 )
10 11	

The second option - to use a dictionary unpacking easier to read and less likely to go wrong, and about 70% faster.
Micro-channel public number "full-stack learning nest"

Guess you like

Origin blog.csdn.net/gzyiCG/article/details/91957863