代码段:
def args_test(*args):
print "args:", args
for value in args:
if type(value) is str:
print "value: %s" % value
elif type(value) is int:
print "value: %d" % value
else:
print "hehe"
print "第二个:", args[1]
def kwargs_test(**kwargs):
print "kwargs:", kwargs
for key in kwargs:
print "第%s个: %d" % (key, kwargs[key])
print "第二个:", kwargs['b']
if __name__ == "__main__":
args_test(1, 2, 3, 4, 5)
print "--------------------------------------1"
list = [1, 2, 3, 4]
args_test(*list)
print "--------------------------------------2"
tuple = (1, 2, 3, 4)
args_test(*tuple)
print "--------------------------------------3"
dict = {'a': 7, 'b': 8, 'c': 9, 'd': 10}
args_test(*dict)
print "--------------------------------------4"
kwargs_test(a=1, b=2, c=3, d=4, e=5)
print "--------------------------------------5"
dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
kwargs_test(**dict)
print "--------------------------------------6"
tuple = (1, 2, 3, 4, 5)
kwargs_test(**tuple)
print "--------------------------------------7"
list = [1, 2, 3, 4, 5]
kwargs_test(**list)
print "--------------------------------------8"
运行结果
![这里写图片描述](https://img-blog.csdn.net/20161228112321766?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvR1pIZXJtaXQ=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
![这里写图片描述](https://img-blog.csdn.net/20161228112351304?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvR1pIZXJtaXQ=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)