现在的位置: 首页 > 综合 > 正文

非关键字可变长参数(元组)

2018年04月30日 ⁄ 综合 ⁄ 共 407字 ⁄ 字号 评论关闭
可变参数定义的时候,前边加上*

函数里可以当做元组读取可变参数

例子:

def tupleVarArgs(arg1,arg2='defaultB',*theRest):
    print 'formal arg 1:',arg1
    print 'formal arg 2:',arg2
    for eachXtrArg in theRest:
        print 'another arg:',eachXtrArg

>>> tupleVarArgs('abc')
formal arg 1: abc
formal arg 2: defaultB
>>> tupleVarArgs(23,4.23)
formal arg 1: 23
formal arg 2: 4.23
>>> tupleVarArgs('abc',123,'xyz',344.23)
formal arg 1: abc
formal arg 2: 123
another arg: xyz
another arg: 344.23
>>>

可变参数数组

抱歉!评论已关闭.