函数参数个数未知案例
例如:返回任意数量整数之和
def main(*numlist):
Sum=0
count=0
while count <len(numlist):
Sum+= int(numlist[count])
count+=1
return Sum
if __name__=='__main__':
print(main(1,4,7,3,9))
print(main(1,3,5))
其中形参*numlist表示一个empty tuple,元素类型为string
由此拓展
按位置传参:
def addint(a,b):
return a+b
if __name__=='__main__':
s = [1,2]
l = (1,2)
print(addint(*s))
print(addint(*l))
注意:此时函数形参的个数必须和list(set)元素个数一致