A, Python function parameters
1, when using a function of python with parameter types, parameters such as location, keyword parameters, a variable length parameter
2, location parameters, keyword parameters are well understood, the key is a variable-length parameters can often see, but have not get to know what it means
Two, variable length parameters
1, an asterisk : function may receive any number of arguments, only need to add an asterisk (*) in front of the parameter, an asterisk will function parameter values as a plurality of positional parameters passed in the form of tuples, i.e. a plurality of parameter values can be passed inside tuples traversal function
def length_param(a, *args): print("a=", a) print("args=", args) for arg in args: print("arg=", arg) length_param("zim","this","is","a","good","thing")
Results of the:
2, two asterisks: parameter preceded by two * (asterisk), note the two asterisks Oh, will function key parameter values two asterisks formal parameters as in the form of a dictionary passed in the function internal dictionary as keyword arguments will be traversed within the function
def length_param(a, **kwargs):
print("a=", a)
print("kwargs=", kwargs)
for kwarg in kwargs.keys():
print("kwarg=", kwarg)
length_param("zim",b="this",c="is",d="good")
Results of the:
3, an asterisk and two asterisks mix
class Params:
def length_param(self,*args,**kwargs):
print("args=",args)
print("kwargs=",kwargs)
for arg in args:
print("arg=",arg) for kwarg in kwargs.values(): print("kwarg=",kwarg) one = Params() one.length_param("sam","this","is","good","thing",b="you",c="love",d="me")
Results of the:
Note: When passing a variable-length parameters, passing keyword arguments key must not have colon, otherwise they will be reported the following error
Third, pay attention:
1, when the function is called keyword arguments must be back in the position parameter