经常在看一些模块的时候,发现*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_argfor pos, arg in argv.iteritems():print "another arg through {0} in {1}:".format(arg, pos)>>> test_var_args('aa', name='python')first normal arg: aaanother arg through python in name:>>> kwargs = {"arg3": 3, "arg2": "two","arg1":5}>>> test_var_args('aa', **kwargs)first normal arg: aaanother arg through 5 in arg1:another arg through two in arg2:another arg through 3 in arg3:
最后
以上就是时尚时光最近收集整理的关于python *arg, **arg的全部内容,更多相关python内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复