python中的*arg **kwargs
2015-06-19 16:19
806 查看
在python中,这两个是python中的可变参数,*arg表示任意多个无名参数,类型为tuple,**kwargs表示关键字参数,为dict,使用时需将*arg放在**kwargs之前,否则会有“SyntaxError: non-keyword arg after keyword arg”的语法错误 Python代码
def test(*arg,**kwargs):
print arg
print kwargs
print "-------------------"
if __name__=='__main__':
test(1,2,3,4,5)
test(a=1,b=2,c=3)
test(1,2,3,a=1,b=3,c=5)
output:
(1, 2, 3, 4, 5)
{}
-------------------
()
{'a': 1, 'c': 3, 'b': 2}
-------------------
(1, 2, 3)
{'a': 1, 'c': 5, 'b': 3}
-------------------
def test(*arg,**kwargs):
print arg
print kwargs
print "-------------------"
if __name__=='__main__':
test(1,2,3,4,5)
test(a=1,b=2,c=3)
test(1,2,3,a=1,b=3,c=5)
output:
(1, 2, 3, 4, 5)
{}
-------------------
()
{'a': 1, 'c': 3, 'b': 2}
-------------------
(1, 2, 3)
{'a': 1, 'c': 5, 'b': 3}
-------------------
相关文章推荐
- Python性能鸡汤
- Python性能鸡汤
- python调用word报错,提示“服务器运行失败”,现已解决
- Python的不定长参数研究
- python判断字符串
- python datetime对象转时间戳
- python替换html实体的方法
- python 的对象序列化/反序列化
- python替换html实体的方法
- python 的对象序列化/反序列化
- python 获取一个字符串所有大小写的情况
- python 文件的md5
- python转换日志中的时间格式
- coreseek python数据源支持的字段类型
- python开发工具 Wing IDE 简单配置以及中文乱码问题解决
- #!/usr/bin/env python 和 #!/usr/bin/python 的区别
- python内存不释放原理
- Python快速教程
- python subprocess命令行调用py文件 报错EOFError: EOF when reading a line (已解决)
- 使用meliae来做Python对象内存监测