[转]Python的函数参数形式

作者: aries 分类: Python 发布时间: 2012-10-25 21:40 ė 1169次浏览 6 0评论

Python函数参数可以用星号+形参名形式(arg或**arg)方式。其中arg方式必须在传统参数后边出现,而**arg则必须在*arg后出现。

例如:
def test(x, y=5, *a, **b):
    print x, y, a, b

函数调用结果:

test(1) ===> 1 5 () {}
test(1,2) ===> 1 2 () {}
test(1,2,3)  ===> 1 2 (3,) {}
test(1,2,3,4) ===> 1 2 (3,4)
test(x=1)   ===> 1 5 () {}
test(x=1,y=1)  ===> 1 1 () {}
test(x=1,y=1,a=1)   ===> 1 1 () {'a':1}
test(x=1,y=1,a=1,b=1)   ===> 1 1 () {'a':1,'b':1}
test(1,y=1) ===> 1 1 () {}
test(1,2,y=1) ===> 出错说y给赋了多个值
test(1,2,3,4,a=1) ===> 1 2 (3,4) {'a':1}
test(1,2,3,4,k=1,t=2,o=3) ===> 1 2 (3,4) {'k':1,'t':2,'o':3}
换一个
暂无评论
Ɣ回顶部