Python 学习入门(17)—— args, kwargs

The special syntax, *args and **kwargs in function definitions is used to pass a variable number of arguments to a function. The single asterisk form (*args) is used to pass a non-keyworded, variable-length argument list, and the double asterisk form is used to pass a keyworded, variable-length argument list. Here is an example of how to use the non-keyworded form. This example passes one formal (positional) argument, and two more variable length arguments.

def test_var_args(farg, *args):
    print "formal arg:", farg
    for arg in args:
        print "another arg:", arg

test_var_args(1, "two", 3)

Results:

formal arg: 1
another arg: two
another arg: 3

Here is an example of how to use the keyworded form. Again, one formal argument and two keyworded variable arguments are passed.

def test_var_kwargs(farg, **kwargs):
    print "formal arg:", farg
    for key in kwargs:
        print "another keyword arg: %s: %s" % (key, kwargs[key])

test_var_kwargs(farg=1, myarg2="two", myarg3=3)

Results:

formal arg: 1
another keyword arg: myarg2: two
another keyword arg: myarg3: 3

Using *args and **kwargs when calling a function

This special syntax can be used, not only in function definitions, but also when calling a function.

def test_var_args_call(arg1, arg2, arg3):
    print "arg1:", arg1
    print "arg2:", arg2
    print "arg3:", arg3

args = ("two", 3)
test_var_args_call(1, *args)

Results:

arg1: 1
arg2: two
arg3: 3

Here is an example using the keyworded form when calling a function:

def test_var_args_call(arg1, arg2, arg3):
    print "arg1:", arg1
    print "arg2:", arg2
    print "arg3:", arg3

kwargs = {"arg3": 3, "arg2": "two"}
test_var_args_call(1, **kwargs)

Results:

arg1: 1
arg2: two
arg3: 3

代码示例
[python] view plain copy print ?
  1. #!/usr/bin/python  
  2. # -*- coding:utf-8 -*-  
  3. #  
  4. # http://blog.ithomer.net  
  5.   
  6.   
  7. # *args  
  8. def test1(farg, *args):  
  9.     print "formal arg", farg  
  10.     for arg in args:  
  11.         print "another arg:", arg  
  12.   
  13. # **kwargs  
  14. def test2(farg, **kwargs):  
  15.     print "formal arg", farg  
  16.     for key in kwargs:  
  17.         print "another keyword arg: %s, %s" % (key, kwargs[key])  
  18.   
  19. # *args  
  20. def test3(arg1, arg2, arg3):  
  21.     print "arg1:", arg1  
  22.     print "arg2:", arg2  
  23.     print "arg3:", arg3  
  24.   
  25. # **kwargs  
  26. def test4(arg1, arg2, arg3):  
  27.     print "arg1:", arg1  
  28.     print "arg2:", arg2  
  29.     print "arg3:", arg3  
  30.   
  31.   
  32. # *args  
  33. def test5(*args):  
  34.     for count, thing in enumerate(args):  
  35.         print "%d -> %s" % (count, thing)  
  36.   
  37. # **kwargs  
  38. def test6(**kwargs):  
  39.     for name, value in kwargs.items():  
  40.         print name, "=", value  
  41.   
  42. def test7(a, b, c):  
  43.     print "a=",a, "& b=", b, "& c=", c  
  44.   
  45. if __name__ == "__main__":  
  46.     test1(1"two"3)  
  47.     test2(farg=1, myarg2='two', myargs=3)  
  48.   
  49.     args = ("two"3)  
  50.     test3(1, *args)  
  51.   
  52.     kwargs = {"arg3":3"arg2":"two"}  
  53.     #test4(1, **kwargs)  
  54.     test4(arg1=1, **kwargs)  
  55.   
  56.   
  57.     test5("apple""banana""cabbage")  
  58.     test6(apple="fruit", cabbage="vagetable")  
  59.   
  60.     mylist = ['aa''bb''cc']  
  61.     test7(*mylist)  
运行结果:

formal arg 1
another arg: two
another arg: 3
formal arg 1
another keyword arg: myarg2, two
another keyword arg: myargs, 3

arg1: 1
arg2: two
arg3: 3
arg1: 1
arg2: two
arg3: 3

0 -> apple
1 -> banana
2 -> cabbage
cabbage = vagetable
apple = fruit
a= aa & b= bb & c= cc



参考推荐:

你可能感兴趣的:(Python 学习入门(17)—— args, kwargs)