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.的更多相关文章

  1. Python函数可变参数*args及**kwargs详解

    初学Python的同学们看到代码中类似func(*args, **kwargs)这样的函数参数定义时,经常感到一头雾水. 下面通过一个简单的例子来详细解释下Python函数可变参数*args及**kw ...

  2. Python中 * 与 **, *args 与 **kwargs的用法

    * 用于传递位置参数(positional argument) ** 用于传递关键字参数(keyword argument) 首先,先通过一个简单的例子来介绍 * 的用法: def add_funct ...

  3. [翻译]PYTHON中如何使用*ARGS和**KWARGS

    [翻译]Python中如何使用*args和**kwargs 函数定义 函数调用 不知道有没有人翻译了,看到了,很短,顺手一翻 原文地址 入口 或者可以叫做,在Python中如何使用可变长参数列表 函数 ...

  4. [转]Python tips: 什么是*args和**kwargs?

    Python tips: 什么是*args和**kwargs? 原文地址:http://www.cnblogs.com/fengmk2/archive/2008/04/21/1163766.html ...

  5. python之动态参数 *args,**kwargs和命名空间

    一.函数的动态参数 *args,**kwargs, 形参的顺序1.你的函数,为了拓展,对于传入的实参数量应该是不固定,所以就需要用到万能参数,动态参数,*args, **kwargs 1,*args ...

  6. 【Python基础】*args,**args的详细用法

     Python基础知识:*args,**args的详细用法 参考:https://blog.csdn.net/qq_29287973/article/details/78040291 *args 不定 ...

  7. 【转载】Python tips: 什么是*args和**kwargs?

    转自Python tips: 什么是*args和**kwargs? 先来看个例子: def foo(*args, **kwargs): print 'args = ', args print 'kwa ...

  8. Python代码中func(*args, **kwargs)

    这是Python函数可变参数 args及kwargs *args表示任何多个无名参数,它是一个tuple **kwargs表示关键字参数,它是一个dict 测试代码如下: def foo(*args, ...

  9. python 中的 args,*args,**kwargs的区别

    一.*args的使用方法 *args 用来将参数打包成tuple给函数体调用 例子一:def function(*args):      print(args, type(args))function ...

随机推荐

  1. 利用 ELK系统分析Nginx日志并对数据进行可视化展示

    一.写在前面 结合之前写的一篇文章:Centos7 之安装Logstash ELK stack 日志管理系统,上篇文章主要讲了监控软件的作用以及部署方法.而这篇文章介绍的是单独监控nginx 日志分析 ...

  2. 价值100W的经验分享: 基于JSPatch的iOS应用线上Bug的即时修复方案,附源码.

    限于iOS AppStore的审核机制,一些新的功能的添加或者bug的修复,想做些节日专属的活动等,几乎都是不太可能的.从已有的经验来看,也是有了一些比较常用的解决方案.本文先是会简单说明对比大部分方 ...

  3. sprint3与总结

    backlog-看板-燃尽图-每日立会 github:https://github.com/alfredzhu/team-work 总结:这种团队合作的方式很好,大家在一起沟通,相互交流想法,一起解决 ...

  4. Entity FrameWork 延迟加载本质(二)

    1.对于外键实体而言,EF会在用到这个外键属性的时候,才会去查对应的表.这就是按需加载了... 2.按需加载的缺点:每次调用外键实体的时候,都会去查询数据库(EF有小优化:相同的外键实体只查一次) I ...

  5. Java集合Iterator迭代器的实现

    一.迭代器概述 1.什么是迭代器? 在Java中,有很多的数据容器,对于这些的操作有很多的共性.Java采用了迭代器来为各种容器提供了公共的操作接口.这样使得对容器的遍历操作与其具体的底层实现相隔离, ...

  6. Python基础:数值(布尔型、整型、长整型、浮点型、复数)

    一.概述 Python中的 数值类型(Numeric Types)共有5种:布尔型(bool).整型(int).长整型(long).浮点型(float)和复数(complex). 数值类型支持的主要操 ...

  7. MySQL数据库 安装图解

    下面的是MySQL安装的图解,用的可执行文件:下载地址:http://www.jinhusns.com/Products/Download/?type=xcj相关下载 mysql安装向导启动,按“Ne ...

  8. Microsecond and Millisecond C# Timer[转]

    文章转至:http://www.codeproject.com/Articles/98346/Microsecond-and-Millisecond-NET-Timer IntroductionAny ...

  9. 与众不同 windows phone (36) - 8.0 新的瓷贴: FlipTile, CycleTile, IconicTile

    [源码下载] 与众不同 windows phone (36) - 8.0 新的瓷贴: FlipTile, CycleTile, IconicTile 作者:webabcd 介绍与众不同 windows ...

  10. csharp:Nhibernate Procedure with CreateSQLQuery and GetNamedQuery

    <?xml version="1.0" encoding="utf-8"?> <hibernate-mapping assembly=&quo ...