a python Series * args and ** kwargs

1, * args and ** kwargs the difference?
1.1 Overview:
  respectively represent two forms of tuples and dictionaries, as they may be present in the form of parameters also may be present as an actual parameter. Parameter is present as a form referred to as packed, unpacked presence referred to as an actual parameter.

1.2 Description:
1), as a form parameter (packing): Actual parameters packaged position to a tuple, the actual parameters of the package to the keyword dictionary.
def the function name (* args): Packing at the position on the actual parameter tuples
  pass
the function name (actual parameter 1, the actual parameter 2)
EG:

def hobby(name ,age , *hobby):
    for item in hobby:
        print(item)
        
hobby("jmmuy",18,"swimming","pingpang","play games")

 

def the function name (** kwargs): packing parameter in the keyword dictionary
  pass
the function name (parameter 1 = actual key 1, key 2 = actual parameter 2)

eg:

def hobby(**kwargs):
    print(kwargs)
hobby(name='tom',age=18,sex='boy')
 

2), as an actual parameter (unpacking): tuples or expanded dictionary as an actual argument to a formal parameter:

def the function name (in the form of a parameter, the parameter form 2):
  Pass

Tuple = (data 1, data 2)

The function name (* tuple) the tuples correspond to elements in the form of parameters passed to the

 

def function name (name, Age):
  Pass

Dictionary = {name: data 1, age: data 2}


The function name (Dictionary **) in accordance with the key elements in the dictionary correspond to the formal parameters passed

1.3, application scenarios:
1) When implementing decorator, in order for an arbitrary function can be decorated, built-closure function will args, ** kwargs treated with *.
2) define the form of a number of unknown parameters.

Guess you like

Origin www.cnblogs.com/sunshine2017/p/11734420.html