python *args **kwargs用法

来源:互联网 发布:网络视频下载器有那些 编辑:程序博客网 时间:2024/05/16 05:49
# -*- coding:utf-8 -*-def test(*args, **kwargs):    print 'args = ', type(args), args    print 'kwargs = ', type(kwargs), kwargs    print '----end----'if __name__ == '__main__':    test(1, '2', 3)    test(a=1, b='2', c=3)    test(1, 2, 3, a=1, b='2', c=3)    test([1, '2', 3], {'a': 1, 'b': 2}, a=[1, '2', 3], b={'a': 1, 'b': 2})输出结果:args =  <type 'tuple'> (1, '2', 3)kwargs =  <type 'dict'> {}----end----args =  <type 'tuple'> ()kwargs =  <type 'dict'> {'a': 1, 'c': 3, 'b': '2'}----end----args =  <type 'tuple'> (1, 2, 3)kwargs =  <type 'dict'> {'a': 1, 'c': 3, 'b': '2'}----end----args =  <type 'tuple'> ([1, '2', 3], {'a': 1, 'b': 2})kwargs =  <type 'dict'> {'a': [1, '2', 3], 'b': {'a': 1, 'b': 2}}----end----

由输出结果可以看出:
*args将所有输入的value转为tuple
**kwargs将所有输入的key,value转为字典

0 0