经常在看一些模块的时候,发现*arg, **arg这样的
查了一下资料
https://pythontips.com/2013/08/04/args-and-kwargs-in-python-explained/
*arg, **arg的区别是*arg为非keyword的字符,而**arg为keyword字符
举个例子
- >>> def test_var_args(f_arg, *argv):
- ... print "first normal arg:", f_arg
- ... for pos, arg in enumerate(argv):
- ... print "another arg through {0} in {1}:".format(arg, pos)
- ...
- >>> test_var_args('yasoob','python','eggs','test')
- first normal arg: yasoob
- another arg through python in 0:
- another arg through eggs in 1:
- another arg through test in 2:
def test_var_args(f_arg, **argv):
print "first normal arg:", f_arg
for pos, arg in argv.iteritems():
print "another arg through {0} in {1}:".format(arg, pos)
>>> test_var_args('aa', name='python')
first normal arg: aa
another arg through python in name:
>>> kwargs = {"arg3": 3, "arg2": "two","arg1":5}
>>> test_var_args('aa', **kwargs)
first normal arg: aa
another arg through 5 in arg1:
another arg through two in arg2:
another arg through 3 in arg3: