码迷,mamicode.com
首页 > 编程语言 > 详细

python中两个参数的意义 *args **kwargs

时间:2015-09-02 14:49:15      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

 

We often encounter these two parameters.

First *args

This a list of parameters that without name. Baiclly, it is a tuple.

It accepts more than one parameters.

It‘s like Java.  

eg in Java:

class HelloWorld(Object ...name):
      public static void main(String[] args){
            System.out.println(name[0].toString())
    }
}

 

eg:

def test(*args):

  for i in args:

    print i
test(1,2,3)

 

 

Result: you will get 1 2 3

Second **kwargs  keyword 

This is a dict with name.

eg:

def kwargs_test(**kwargs):
    for key, value in kwargs.iteritems():
        print "%s = %s" % (key, value)


kwargs_test(first_name = "jason", last_name = "zhang")
def kwargs_list_test(**kwargs):
    
    print "1--{0}, 2--{1}, 3--{2}".format(a, b, c)


par = [a, b, c]

kwargs_list_test(*par)

 

python中两个参数的意义 *args **kwargs

标签:

原文地址:http://www.cnblogs.com/-Doraemon/p/4778516.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!