Python中的*args和**kwargs
2016-02-26 14:52
676 查看
这部分是关于Python中的函数参数。
《利用Python进行数据分析》:
当你编写func(a, b, c, d=some, e=value)时,位置和关键字参数其实分别是被打包成元组和字典的。函数实际收到的是一个元组args和一个字典kwargs,并在内部完成如下转换:
对于位置参数(args: arguments)被打包成元组,而关键字参数(kwargs: keyword arguments)被打包成字典还是比较好理解的,毕竟位置参数为单值且参数名不会被修改,而关键字参数是(key: value)值对。
后面有个例子:
通过say_hello_then_call_f调用g(在IPython中执行)结果:
需要注意的是:位置参数必须在关键字参数之前,不然会报错。
另一个不明白的地方在于
官方手册:https://docs.python.org/2/tutorial/controlflow.html#keyword-arguments
When a final formal parameter of the form **name is present, it receives a dictionary containing all keyword arguments except for those corresponding to a formal parameter. This may be combined with a formal parameter of the form *name (described in the next subsection) which receives a tuple containing the positional arguments beyond the formal parameter list. (*name must occur before **name.)
也就是说,
当然,关键在于
根据这种打包参数的思想,可以将
《利用Python进行数据分析》:
当你编写func(a, b, c, d=some, e=value)时,位置和关键字参数其实分别是被打包成元组和字典的。函数实际收到的是一个元组args和一个字典kwargs,并在内部完成如下转换:
a, b, c = args d = kwargs.get('d', d_default_value)#如果未给出该值则d值为d_default_value e = kwargs.get('e', e_default_value)
对于位置参数(args: arguments)被打包成元组,而关键字参数(kwargs: keyword arguments)被打包成字典还是比较好理解的,毕竟位置参数为单值且参数名不会被修改,而关键字参数是(key: value)值对。
后面有个例子:
def say_hello_then_call_f(f, *args, **args): print 'args is', args print 'kwargs is', kwargs print("Hello! Now I'm going to call %s" % f) return f(*args, **kwargs) def g(x, y, z=1): return (x + y) / z
通过say_hello_then_call_f调用g(在IPython中执行)结果:
In [166]: say_hello_then_call_f(g, 1, 2, z=5.) args is (1, 2) kwargs is {'z': 5.0} Hello! Now I'm going to call <function g at 0x124eb8410> Out[166]: 0.6
需要注意的是:位置参数必须在关键字参数之前,不然会报错。
另一个不明白的地方在于
*args和
**kwargs的使用,由于之前理解为取args地址处的值,那么kwargs前面的两个’*’就无法解释,于是在stackoverflow找到了Python中关于
*和
**用法的解释:What does ** (double star) and * (star) do for Python parameters?
官方手册:https://docs.python.org/2/tutorial/controlflow.html#keyword-arguments
When a final formal parameter of the form **name is present, it receives a dictionary containing all keyword arguments except for those corresponding to a formal parameter. This may be combined with a formal parameter of the form *name (described in the next subsection) which receives a tuple containing the positional arguments beyond the formal parameter list. (*name must occur before **name.)
也就是说,
*args将位置参数打包成tuple,
**Kwargs将除所有关键字参数打包成字典,它们可以看做Python中的常用语,还可以和普通参数混合使用:
def foo(kind, *args, **kwargs): pass
当然,关键在于
*和
**的区别,是用来区分位置参数和关键字参数,后面命名不一定要args和kwargs,但是这两个词更常用。
根据这种打包参数的思想,可以将
*和
**用于一次传递多个参数:
In [169]: def foo1(x, y, z): .....: print "x=" + str(x) .....: print "y=" + str(y) .....: print "z=" + str(z) .....: In [170]: my_list = [1, 2, 3] In [171]: foo1(*my_list) x=1 y=2 z=3 In [173]: my_dict = {'x':1, 'y':2, 'z':3} In [174]: foo1(**my_dict) x=1 y=2 z=3
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法