【python学习笔记】Python中*args 和**kwargs的用法
2017-09-20 14:16
381 查看
说明
python中默认缺省参数,参数名传参,可变长度传参。缺省参数
def foo(a,b=8): print a,b foo("leason") #leason 0 foo("leason",88) #def 88
参数名传参数
def foo(a,b): print a,b foo("hello",'world') # helloworld foo(b='world',a="hello") # helloworld
可变长度参数
def foo(arg1,*tupleArg,**dictArg): print "arg1=",arg1 #arg1=leason print "tupleArg=",tupleArg #() print "dictArg=",dictArg #[] foo("leason")
可变长度参数*args
# 接收不定长参数*args def fun_var_args(farg, *args): print "arg:", farg for value in args: print "another arg:", value fun_var_args(1, 2, 3) # *args可以当作可容纳多个变量组成的list << arg: 1 << another arg: 2 << another arg: 3 # 传收不定长参数*args def fun_var_args_call(arg1, arg2, arg3): print "arg1:", arg1 print "arg2:", arg2 print "arg3:", arg3 args = [2, 3] #list fun_var_args_call(1, *args) << arg1: 1 << arg2: 2 << arg3: 3
可变长度**kwargs
# 接收不定长参数**kwargs def fun_var_kwargs(arg1, **kwargs): print "arg1:", arg1 for key in kwargs: print "%s: %s" % (key, kwargs[key]) fun_var_kwargs(arg1=1, arg2=2, arg3=3) # **kwargs可以当作容纳多个key和value的dict << arg1: 1 << arg2: 2 << arg3: 3 # 传收不定长参数**kwargs def fun_var_args_call(arg1, arg2, arg3): print "arg1:", arg1 print "arg2:", arg2 print "arg3:", arg3 kwargs = {"arg3": 3, "arg2": 2} # dictionary fun_var_args_call(1, **kwargs) << arg1: 1 << arg2: 2 << arg3: 3
leason | 个人博客
相关文章推荐
- python中*args 和 **kwargs 的用法学习笔记
- Python学习笔记-tips: 什么是*args和**kwargs?
- [Python学习笔记-004] 可变参数*args和**kwargs
- Python学习笔记(3): 什么是*args和**kwargs?
- python学习笔记:*args和**kwargs使用原理?
- Python学习笔记——*args **kwargs 理解
- Python学习笔记(5):lambda,filter,map,reduce,zip用法整理
- 【Python】Python中*args 和**kwargs的用法
- Python中的*args和**kwargs的用法
- python笔记--*args 和 **kwargs
- Python中*args 和**kwargs的用法
- Python学习笔记(7):global关键字及全局变量的用法
- Python学习笔记(4)Python中super的用法
- python中 *args和**kwargs的基本用法
- Python学习之*args和**kwargs
- Python 学习入门(17)—— args, kwargs
- Python中 *args 和 **kwargs的用法
- python中-args 和--kwargs的用法
- Python中*args 和**kwargs的用法
- Python学习:纠错笔记:详解os.removedirs(path)的正确用法