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 ...
随机推荐
- Identity标识列
SQL Server中,经常会用到Identity标识列,这种自增长的字段操作起来的确是比较方便.但它有时还会带来一些麻烦. 示例一 :当表中被删除了某些数据的时候,自增长列的编号就不再是一个连线的数 ...
- LINQ TO XML 个人的一些心得1
最近没事做,刚来到一个新公司.写了一些处理xml的项目 就是把一些xml的数据处理后存储到数据库中.原本还是准备用原来的xml来写的.在群里有个人说,用linq to xml 好了,比较快捷.就看了 ...
- C#中 导入和导出Excel的方法
using System.Data; using System.Data.OleDb; /// <summary> /// Excel转为DataTable /// </summar ...
- python 的import机制2
http://blog.csdn.net/sirodeng/article/details/17095591 python 的import机制,以备忘: python中,每个py文件被称之为模块, ...
- javascript url几种编码方式
1.escape() 不能直接用于URL编码,它的真正作用是返回一个字符的Unicode编码值.比如“春节”的返回结果是%u6625%u8282,escape()不对"+"编码主要 ...
- platform总线globalfifo驱动
功能是使用内存的4k单元,实现读,写,偏移,清除. /************************************************************************* ...
- Codeforces Round #346 (Div. 2)---E. New Reform--- 并查集(或连通图)
Codeforces Round #346 (Div. 2)---E. New Reform E. New Reform time limit per test 1 second memory lim ...
- Hibernate框架的总结
hibernate 简介:hibernate是一个开源框架,它是对象关联关系映射的框架,它对JDBC做了轻量级的封装,而我们java程序员可以使用面向对象的思想来操纵数据库.hibernate核心接口 ...
- css中的定位和框模型问题
和定位有关的元素属性如下 position 元素的定位类型 绝对定位会相对于最近定位的祖先元素的位置来定位,而不会影响其他框的位置 固定定位 相对定位 z-index 元素的堆叠顺序 值越大 ...
- 发布ASP.NET Core程序到Linux生产环境
原文翻译:Publish to a Linux Production Environment 作者:Sourabh Shirhatti 在这篇文章里我们将介绍如何在 Ubuntu 14.04 Serv ...