python可变参数的使用注意

370次阅读
没有评论

python可变参数的使用注意

python函数传入实参,可变参数(*)之前的参数不能指定参数名。

>>> def myfun(a, *b):
...     print(a
)...     print(b)
...
>>> myfun(a=1, 2,3,4)  
File "<stdin>", line 1
SyntaxError: positional argument follows keyword argument
>>> myfun(1, 2,3,4)
1
(2, 3, 4)

python函数传入实参,可变参数(*)之后的参数必须指定参数名,否则就会被归到可变参数之中。

>>> def myfun(a, *b, c=None):
...     print(a)
...     print(b)
...     print(c)
...
>>> myfun(1, 2,3,4)
1
(2, 3, 4)
None
>>> myfun(1, 2,3,c=4)
1
(2, 3)
4
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2021-04-30发表,共计409字。
新手QQ群:570568346,欢迎进群讨论 Python51学习