* Summarize and understand the arguments args and ** kwargs function

A: Function argument understanding:

def function name (function parameters): 
    function body 

, for example: 
def FUNC (A):   # A is a parameter 
    Print (A) 

FUNC ( 123)   # 123 is the argument 

parameter is divided into: a keyword parameter, a position parameter, mixed parameter 
keyword parameter 
DEF FUNC (X, Y, Z = " default parameters " ):
     Print (X, Y, Z) 

FUNC ( 1, Y = " xumou " )   # 1 xumou default parameters 
' '' 
represents a position parameter, 
y represents the keyword parameter 
z represents the default parameters of 

the relationship between them: keyword parameter must be behind the position parameter, 

a position parameter> keyword parameters, the default value of the parameter 

'' ' 
# when the received dynamic parameters, the dynamic parameters have to be in the rear position parameter 
deffunc01 (A, B, * GG): 

    Print (GG, A, B) 

func01 ( 1,2, " 34 is " ) 

# position parameter and default values of the parameters: position parameter must be declared, then declare a default value of the parameter 
DEF func03 ( a, C = 12 is ): 

    Print (a, C) 

func03 ( " AA " )   #   AA 12 is 


# * PP * in receiving any of the dynamic parameters: 
DEF func03 (* PP):
     Print (PP) 
func03 ( " Q " , " W " , " E " , 22,33)   # ( 'Q', 'W', 'E', 22 is, 33 is) 


#Accept dynamic parameters: positional parameters must be in the dynamic parameter 

DEF func04 (A, b, * args):
     Print ( " func04 >> " , A, b, args) 
func04 ( 1,2,5,7)   # func04 >> 12 (5, 7) 

# ---------- error model: ---------- 
DEF func04 (* args, A, B):
     Print ( " func04 >> " , A, B, args) 
func04 ( 1,2,5,7)   # error 

# ------- 

# error: because of the changed: 
DEF func04 ( * args, A, b):
     Print ( " func04 >> " , A, b, args)
func04 ( 1,2,. 5 A =, B =. 7)   # func04 >>. 5. 7 (. 1, 2) 



# ** kwargs for receiving dynamic parameters keyword 
DEF func02 (** kwargs):
     Print (kwargs)   #   { 'a': 12, 'b ': 23} the result is a dict 

func02 (a = 12 is, B = 23 is ) 


# final order is: 
# position parameter> * args> default parameters> ** kwargs

 

Guess you like

Origin www.cnblogs.com/one-tom/p/11263786.html