argv:
命令行参数List,第一个元素是程序本身路径,如需获取参数可用argv[1:]
*args and * kwargs
简单地说:*args去匹配没有指定参数名的参数,即列举出来的参数agr1,arg2,agr3。**kwargs 去匹配指定参数名的参数,形如:arg1="arg1",arg2="arg2",arg3="arg3"
关于*args and * kwargs的使用看下面几个例子:
>>> def print_everything(*args): for count,thing in enumerate(args): ... print "%d. %s" %(count, thing) ... >>> print_everything('apple', 'banana', 'cabbage') 0. apple 1. banana 2. cabbage
上面这个例子,对*args进行解析的时候用到了“for a, b in enumerate (data)”. 这里data是list或者tuple, a则自动对应index, b自动对应value。
>>> def table_things(**kwargs): ... for name,value in kwargs.items(): ... print name, "=", value ... >>> table_things(apple = 'fruit', cabbage = 'vegetable') cabbage = vegetable apple = fruit
这个例子中显然参数是一个具有key和value的键值对,访问的时候需要用到“for a, b in dic.items()”.
>>> def print_three_things(a, b, c): ... print "a =", a, "& b =", b, "& c =", c ... >>> mylist = ['aardvark', 'baboon', 'cat'] >>> print_three_things(*mylist) a = aardvark & b = baboon & c = cat
请看下面列子,就能明白这两个参数的含义:
>>> def printargs(*arg,**kwargs):
print arg,
print kwargs
>>> printargs("a","b",c="1",d="2")
('a', 'b') {'c': '1', 'd': '2'}
>>>