def total(initial=5, *numbers, vegetables):
count = initial
for number in numbers:
count += number
count += vegetables #这一行为什么不需要for语句呢?
return count
print total(10, 1, 2, 3, vegetables=50)
运行结果是语法错误,keyword-only是需要在带*参数后面的没错吧,检查好久都看不出来哪出问题了呢?(还有,第5行为什么是可以不需要for语句的呢?初学小白,求教,谢谢!)
py3.4表示并没有错误...
vegetables就是keyword-only参数,10是initial,(1,2,3)是可变参数;
至于count+=vegetables只进行一次运算,并不是number元组里面的循环;
参数顺序有问题total(vegetables, initial=5, *numbers)
- positional参数只能放在其他类型参数的前面
- 带默认值的参数后面只能跟*arg或/和**keyword
- *arg后面只能跟**keyword
- **keyword只能放最后