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. onhashchange事件,只需要修改hash值即可响应onhashchange事件中的函数(适用于上一题下一题和跳转页面等功能)

    使用实例: 使用onhashchange事件做一个简单的上一页下一页功能,并且当刷新页面时停留在当前页 html: <!DOCTYPE html><html><body& ...

  2. CSS3魔法堂:禁止用户改变textarea大小

    一.前言 在FF.Chrome和Safari下默认时允许用户以拖拽形式来改变textarea大小,这不仅与IE下textarea的行为特点有异,而且textarea的大小变化会撑大其父节点从而破坏整体 ...

  3. JS魔法堂:那些困扰你的DOM集合类型

    一.前言 大家先看看下面的js,猜猜结果会怎样吧! 可选答案: ①. 获取id属性值为id的节点元素 ②. 抛namedItem is undefined的异常 var nodes = documen ...

  4. 第一个sprint心得及感想

    经过两个星期的努力,第一个周期的任务终于完成,通过这次团队协作,学到了很多东西,首先是把任务细分化,把大的任务分为每天完成,然后团队个人都有自己的任务份额,这样子就不会全压在一个人身上.还有就是学会了 ...

  5. czxt

    实验三 进程调度模拟程序 1.    目的和要求 1.1.           实验目的 用高级语言完成一个进程调度程序,以加深对进程的概念及进程调度算法的理解. 1.2.           实验要 ...

  6. 区间合并 --- Codeforces 558D : Gess Your Way Out ! II

    D. Guess Your Way Out! II Problem's Link: http://codeforces.com/problemset/problem/558/D Mean: 一棵满二叉 ...

  7. 【JavaScript回顾】继承

    组合继承 组合继承(combination inheritance),有时候也叫做伪经典继承,指的是将原型链和借用构造函数的 技术组合到一块,从而发挥二者之长的一种继承模式.其背后的思路是使用原型链实 ...

  8. WebApi传参总动员(填坑)

    本以为系列文章已经Over,突然记起来前面留了个大坑还没填,真是自己给自己挖坑. 这个坑就是: (body 只能被读取一次)Only one thing can read the body MVC和W ...

  9. 【C#进阶系列】01 CLR的执行模型——一个Hello World的故事

    好吧,废话少说,先上一章Hello World图: 我们有了一个Hello world程序,如此之简单,再加上我今天没有用汉字编程o(>﹏<)o,所以一切很简单明了. 故事开始: 编译: ...

  10. Error generating Swagger server (Python Flask) from Swagger editor

    1down votefavorite   http://stackoverflow.com/questions/36416679/error-generating-swagger-server-pyt ...