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:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值