Python中*args 和**kwargs的用法
2017-12-15 15:42
411 查看
当函数的参数不确定时,可以使用*args 和**kwargs,*args 没有key值,**kwargs有key值。
还是直接来代码吧,废话少说
[python] view plain copy print?def fun_var_args(farg, *args):
print “arg:”, farg
for value in args:
print “another arg:”, value
fun_var_args(1, “two”, 3) # *args可以当作可容纳多个变量组成的list
[python] view plain copy print?arg: 1
another arg: two
another arg: 3
**kwargs:
[python] view plain copy print?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和myarg3被视为key, 感觉**kwargs可以当作容纳多个key和value的dictionary
result:
[python] view plain copy print?arg: 1
another keyword arg: myarg2: two
another keyword arg: myarg3: 3
[python] view plain copy print?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)
[python] view plain copy print?arg1: 1
arg2: two
arg3: 3
[python] view plain copy print?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)
[python] view plain copy print?arg1: 1
arg2:”two”
arg3:3
原文地址:http://blog.csdn.net/chenjinyu_tang/article/details/8136841
还是直接来代码吧,废话少说
[python] view plain copy print?def fun_var_args(farg, *args):
print “arg:”, farg
for value in args:
print “another arg:”, value
fun_var_args(1, “two”, 3) # *args可以当作可容纳多个变量组成的list
def fun_var_args(farg, *args): print "arg:", farg for value in args: print "another arg:", value fun_var_args(1, "two", 3) # *args可以当作可容纳多个变量组成的listresult:
[python] view plain copy print?arg: 1
another arg: two
another arg: 3
arg: 1 another arg: two another arg: 3
**kwargs:
[python] view plain copy print?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和myarg3被视为key, 感觉**kwargs可以当作容纳多个key和value的dictionary
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和myarg3被视为key, 感觉**kwargs可以当作容纳多个key和value的dictionary
result:
[python] view plain copy print?arg: 1
another keyword arg: myarg2: two
another keyword arg: myarg3: 3
arg: 1 another keyword arg: myarg2: two another keyword arg: myarg3: 3也可以用下面的形式:
[python] view plain copy print?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)
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:
[python] view plain copy print?arg1: 1
arg2: two
arg3: 3
arg1: 1 arg2: two arg3: 3
[python] view plain copy print?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)
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:
[python] view plain copy print?arg1: 1
arg2:”two”
arg3:3
arg1: 1 arg2:"two" arg3:3
原文地址:http://blog.csdn.net/chenjinyu_tang/article/details/8136841
相关文章推荐
- python中的含有 *args 和**kwargs的用法 作为参数的函数,参数输入问题
- Python中*args 和**kwargs的用法
- Python中*args 和**kwargs的用法
- Python中*args 和**kwargs的用法
- Python中*args 和**kwargs的用法
- Python中*args 和**kwargs的用法
- Python中*args 和**kwargs的用法
- Python 进阶----*args和 **kwargs,装饰器 的用法
- Python中的*args和**kwargs的理解与用法
- Python中*args 和**kwargs的用法
- Python中*args 和**kwargs的用法探讨
- Python中*args 和**kwargs的用法
- Python中*args 和**kwargs的用法
- Python中的*args和**kwargs的用法
- 【python】Python中*args 和**kwargs的用法
- Python中*args 和**kwargs的用法
- Python中*args 和**kwargs的用法
- 【Python】Python中*args 和**kwargs的用法
- 【python】args kwargs 用法
- Python中*args 和**kwargs的用法