|
Args is an array of all parameters, kwargs is that when you pass key = value is stored in the dictionary, when the function parameter uncertainty, you can use the * args and kwargs, * args no key value, kwargs have key value
def fun_var_args (farg, * args):
print "arg:", farg
for value in args:
print "another arg:", value
fun_var_args (1, "two", 3) # * args can accommodate multiple variables can be used as component list
result:
arg: 1
another arg: two
another arg: 3
** Kwargs:
def fun_var_kwargs (farg, ** kwargs):
print "arg:", farg
for key in kwargs:
print "another keyword arg:% s:% s"% (key, kwargs [key])
fun_var_kwargs (farg = 1, myarg2 = "two", myarg3 = 3) # myarg2 and myarg3 is considered key, feel ** kwargs can accommodate a plurality of key and value as the dictionary
result:
arg: 1
another keyword arg: myarg2: two
another keyword arg: myarg3: 3
You can also use the following form:
def fun_var_args_call (arg1, arg2, arg3):
print "arg1:", arg1
print "arg2:", arg2
print "arg3:", arg3
args = [ "two", 3] #list
fun_var_args_call (1, * args)
result:
arg1: 1
arg2: two
arg3: 3
def fun_var_args_call (arg1, arg2, arg3):
print "arg1:", arg1
print "arg2:", arg2
print "arg3:", arg3
kwargs = { "arg3": 3, "arg2": "two"} # dictionary
fun_var_args_call (1, ** kwargs)
result:
arg1: 1
arg2: "two"
arg3: 3
When all the Senate as key = value form will return a dictionary |
|
|
|