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 ...
随机推荐
- Appium移动自动化测试(三)--安装Android模拟器
当Android SDK安装完成之后,并不意味着已经装好了安装模拟器.Android系统有多个版本,所以我们需要选择一个版本进行安装. 第三节 安装Android 模拟器 我这里以Android 4 ...
- Javascript面向对象之继承
与类的创建篇一样,这里先贴出最终代码,再做详细分析: // 创建一个父类 function SuperType(){ this.company = 'alibaba'; } function SubT ...
- EFcodeFirst+T4=操纵任意数据库
之前有写过两篇,EF选择Mysql数据源 跟 EF添加ADO.NET实体模型处直接选择Oracle数据源,其方便之处就不多说了,使用DBfirst直接点点点就能与数据库双向更新,而且关键是方便我们使用 ...
- Scrum团队
5.Scrum团队成立 5.1 团队名称,团队目标.团队口号.团队照: 团队名称:@four! 团队目标:做出像“数学口袋精灵”那么棒的软件 团队口号:多劳多得 团队照: 5.2 角色分配 产品负责人 ...
- Mysql的“Limit”操作
Limit操作: ,; #返回第6-15行数据 ; #返回前5行 ,; #返回前5行 性能优化: 基于MySQL5.0中limit的高性能,我对数据分页也重新有了新的认识.测试SQL语句1: Sele ...
- 常见的几种 CSS 水平垂直居中解决办法
用CSS实现元素的水平居中,比较简单,可以设置text-align center,或者设置 margin-left:auto; margin-right:auto 之类的即可. 主要麻烦的地方还是在垂 ...
- 有关CLR的初学小整理(可能理解不深刻,望大牛指出)
1. .Net程序通过CLR去加载运行管理代码, 加载CLR的进程成为“宿主”,通常操作系统加载. 加载CLR的进程也可以为某个DLL,也成为“宿主” 2. 宿主接口使宿主能够对运行库的更多方面进行控 ...
- Matlab绘图(一二三维)
Matlab绘图 强大的绘图功能是Matlab的特点之一,Matlab提供了一系列的绘图函数,用户不需要过多的考虑绘图的细节,只需要给出一些基本参数就能得到所需图形,这类函数称为高层绘图函数.此外,M ...
- C# Web Forms - Using jQuery FullCalendar
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> ...
- 邻接矩阵c源码(构造邻接矩阵,深度优先遍历,广度优先遍历,最小生成树prim,kruskal算法)
matrix.c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include < ...