python *arg, **arg

经常在看一些模块的时候,发现*arg, **arg这样的

查了一下资料

https://pythontips.com/2013/08/04/args-and-kwargs-in-python-explained/

*arg, **arg的区别是*arg为非keyword的字符,而**arg为keyword字符

举个例子

  1. >>> def test_var_args(f_arg, *argv):
  2. ...     print "first normal arg:", f_arg
  3. ...     for pos, arg in enumerate(argv):
  4. ...         print "another arg through {0} in {1}:".format(arg, pos)
  5. ... 
  6. >>> test_var_args('yasoob','python','eggs','test')
  7. first normal arg: yasoob
  8. another arg through python in 0:
  9. another arg through eggs in 1:
  10. another arg through test in 2:

 

  1. def test_var_args(f_arg, **argv):
  2. print "first normal arg:", f_arg
  3. for pos, arg in argv.iteritems():
  4. print "another arg through {0} in {1}:".format(arg, pos)
  5.  
  6.  
  7. >>> test_var_args('aa', name='python')
  8. first normal arg: aa
  9. another arg through python in name:
  10.  
  11. >>> kwargs = {"arg3": 3, "arg2": "two","arg1":5}
  12. >>> test_var_args('aa', **kwargs)
  13. first normal arg: aa
  14. another arg through 5 in arg1:
  15. another arg through two in arg2:
  16. another arg through 3 in arg3:

你可能感兴趣的:(python *arg, **arg)