# 可以将一个可变数量的参数传递给一个函数吗？

``fn(a, b) fn(a, b, c, d, ...)` `

` `def manyArgs(*arg): print "I was called with", len(arg), "arguments:", arg >>> manyArgs(1) I was called with 1 arguments: (1,) >>> manyArgs(1, 2,3) I was called with 3 arguments: (1, 2, 3)` `

` `def myfunc(**kwargs): # kwargs is a dictionary. for k,v in kwargs.iteritems(): print "%s = %s" % (k, v) myfunc(abc=123, efh=456) # abc = 123 # efh = 456` `

` `def myfunc2(*args, **kwargs): for a in args: print a for k,v in kwargs.iteritems(): print "%s = %s" % (k, v) myfunc2(1, 2, 3, banan=123) # 1 # 2 # 3 # banan = 123` `

` `def manyArgs1(args): print args.a, args.b #note args.c is not used here def manyArgs2(args): print args.c #note args.b and .c are not used here class Args: pass args = Args() args.a = 1 args.b = 2 args.c = 3 manyArgs1(args) #outputs 1 2 manyArgs2(args) #outputs 3` `

` `myfuns = [manyArgs1, manyArgs2] for fun in myfuns: fun(args)` `

` `def myfunc(**kwargs): for k,v in kwargs.items(): print ("%s = %s" % (k, v)) myfunc(abc=123, efh=456) # abc = 123 # efh = 456` `

` `def myfunc2(*args, **kwargs): for a in args: print a for k,v in kwargs.items(): print ("%s = %s" % (k, v)) myfunc2(1, 2, 3, banan=123) # 1 # 2 # 3 # banan = 123` `
` `def f(dic): if 'a' in dic: print dic['a'], pass else: print 'None', if 'b' in dic: print dic['b'], pass else: print 'None', if 'c' in dic: print dic['c'], pass else: print 'None', print pass f({}) f({'a':20, 'c':30}) f({'a':20, 'c':30, 'b':'red'}) ____________` `

` `None None None 20 None 30 20 red 30` `