python arguments *args and **args ** is for dictionaries, * is for lists or tuples.
below is a good answer for this question , so I copy on here for some people need it
By the way, the three forms can be combined as follows: def f(a,*b,**c):
All single arguments beyond the first will end up with the tuple b, and all key/value arguments will end up in dictionary c.
For example, f(1,2,3,4,5,d=6,e=7,f=8) should put 1 in a, (2,3,4,5) in b, and {‘d’:6,’e':7,’f':8} in c.
Let’s see if it does. Let’s define f first:
>>> def f(a,*b,**c):print a,b,c
Now, let’s call the function:
>>> f(1,2,3,4,5,d=6,e=7,f=8)
1 (2, 3, 4, 5) {‘e’: 7, ‘d’: 6, ‘f’: 8}
test(x)
test(*x)
test(**x)
>>> x=(1,2,3)
>>> test(x)
Traceback (most recent call last): File “”, line 1, in ?
TypeError: test() takes exactly 3 arguments (1 given)
Again, as expected. We fed a function that expects 3 arguments with only one argument (a tuple with 3 items), and we got an error.
If we want to use the items in the sequence, we use the following form:
>>> test(*x)
1 2 3
There, x was split up, and its members used as the arguments to test. What about the third form?
>>> test(**x)
Traceback (most recent call last): File “”, line 1, in ?
TypeError: test() argument after ** must be a dictionary
It returned an error, because ** always refers to key/value pairs, i.e., dictionaries.
Let’s define a dictionary then:
>>> x={‘a’:1,’b':2,’c':3}
>>> test(**x) 1 2 3
>>> test(*x)
a c b
Ok. The first call passed on the values in the dictionary.
The second call passed on the keys (but in wrong order!).
Remember, ** is for dictionaries, * is for lists or tuples.
python arguments *args and **args ** is for dictionaries, * is for lists or tuples.的更多相关文章
- Python函数可变参数*args及**kwargs详解
初学Python的同学们看到代码中类似func(*args, **kwargs)这样的函数参数定义时,经常感到一头雾水. 下面通过一个简单的例子来详细解释下Python函数可变参数*args及**kw ...
- Python中 * 与 **, *args 与 **kwargs的用法
* 用于传递位置参数(positional argument) ** 用于传递关键字参数(keyword argument) 首先,先通过一个简单的例子来介绍 * 的用法: def add_funct ...
- [翻译]PYTHON中如何使用*ARGS和**KWARGS
[翻译]Python中如何使用*args和**kwargs 函数定义 函数调用 不知道有没有人翻译了,看到了,很短,顺手一翻 原文地址 入口 或者可以叫做,在Python中如何使用可变长参数列表 函数 ...
- [转]Python tips: 什么是*args和**kwargs?
Python tips: 什么是*args和**kwargs? 原文地址:http://www.cnblogs.com/fengmk2/archive/2008/04/21/1163766.html ...
- python之动态参数 *args,**kwargs和命名空间
一.函数的动态参数 *args,**kwargs, 形参的顺序1.你的函数,为了拓展,对于传入的实参数量应该是不固定,所以就需要用到万能参数,动态参数,*args, **kwargs 1,*args ...
- 【Python基础】*args,**args的详细用法
Python基础知识:*args,**args的详细用法 参考:https://blog.csdn.net/qq_29287973/article/details/78040291 *args 不定 ...
- 【转载】Python tips: 什么是*args和**kwargs?
转自Python tips: 什么是*args和**kwargs? 先来看个例子: def foo(*args, **kwargs): print 'args = ', args print 'kwa ...
- Python代码中func(*args, **kwargs)
这是Python函数可变参数 args及kwargs *args表示任何多个无名参数,它是一个tuple **kwargs表示关键字参数,它是一个dict 测试代码如下: def foo(*args, ...
- python 中的 args,*args,**kwargs的区别
一.*args的使用方法 *args 用来将参数打包成tuple给函数体调用 例子一:def function(*args): print(args, type(args))function ...
随机推荐
- 免费下载:320+ 手绘风格 Apple iOS7 图标
Themify 图标是一套用在网页设计和应用程序的图标,包括 320+ 手工制作的像素完美的苹果 iOS7 图标中汲取灵感.这些图标完全免费,您可以用于任何目的,无论是个人或商业. 您可能感兴趣的相 ...
- 你不一定知道的几个很有用的 Git 命令
这里给大家分享一些很有用的 Git 命令,其中很多用法你可能都不知道,无论你是工作在团队环境中或在您的个人项目中,这些命令将对你帮助很大,让你可以更加高效的进行项目开发,更轻松愉快的工作和生活. 您可 ...
- cmd 窗口配置mysql数据库
1.运行-cmd 进入command 窗口 首先cd 到mysql目录下的bin的 路运行-cmd 进入command 窗口 首先cd 到mysql目录下的bin的路径.注意cd D盘时直接输入D:就 ...
- js实现页面a向页面b传参的方法
方法一:使用HTML5本地化存储(localStorage) 组件(本地最大能存储5M数据)localStorage是本地永久存储数据,是cookie的优化 方法二:使用cookie将数据存放在客户的 ...
- cereal:C++实现的开源序列化库
闲来无事发现了一个基于C++实现的序列化工具,相比于其他(比如Boost serialization或Google protobuf,恰巧都用过,以后再介绍),使用简单,感觉不错,下面做个摸索. ce ...
- [JS] javascript基础语法
W3CSchool全套Web开发手册:点击下载 1.javascript是什么 js是具有面向对象能力的,解释性的程序设计语言. 2.js的类型 [基本类型]:string number boolea ...
- CSS行高line-height的理解
一.行高的字面意思 “行高“顾名思义指一行文子的高度.具体来说是指两行文子间基线间的距离. 基线是在英文字母中用到的一个概念,我们刚学英语的时候使用到的那个英语本子每行有4条线,其中底部第二条线就是基 ...
- java内部类的使用
内部类不是很好理解,但说白了其实也就是一个类中还包含着另外一个类 如同一个人是由大脑.肢体.器官等身体结果组成,而内部类相当于其中的某个器官之一,例如心脏:它也有自己的属性和行为(血液.跳动) 显然, ...
- [moka同学摘录]在Centos 6.5下成功安装和配置了vim7.4
来源:https://my.oschina.net/gzyh/blog/266097 资源下载地址: 链接:http://pan.baidu.com/s/1kVuaV5P 密码:xkq9 摘要: ...
- [moka学习笔记]yii2设置语言和时区
1.在web/index.php中 (new yii\web\Application($config))->run(); $app = new \yii\web\Application($con ...