python可变长参数

#!/usr/bin/python2.7
# FileName: test.py
# Author: lxw
# Date: 2015-09-08

def func1(*arg):
    print arg
    print "\n"

def func2(**kw):
    print kw
    print "\n"

def func(*arg, **kw):
    print arg
    print "--" * 10
    print kw
    print "\n"

def main():
    func(1, 2, 3)
    func(1, a = 2, b = 3)
    func(a = 1, b = 2, c = 3)

    func1(1, 2, 3)
    #func1(1, a = 2, b = 3) #Error
    #func1(a = 1, b = 2, c = 3) #Error

    #func2(1, 2, 3) #Error
    #func2(1, a = 2, b = 3) #Error
    func2(a = 1, b = 2, c = 3)


if __name__ == '__main__':
    main()
else:
    print("Being imported as a module.")

'''
Output:
lxw ~$ python test.py
(1, 2, 3)
--------------------
{}


(1,)
--------------------
{'a': 2, 'b': 3}


()
--------------------
{'a': 1, 'c': 3, 'b': 2}


(1, 2, 3)


{'a': 1, 'c': 3, 'b': 2}
'''

 

你可能感兴趣的:(python可变长参数)