ITPub博客

首页 > IT职业 > IT图书 > python可变参数的使用注意

python可变参数的使用注意

IT图书 作者:greatleaf 时间:2021-09-11 09:57:52 0 删除 编辑

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

>>> 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)

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

>>> 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

以上就是python可变参数的使用注意,希望对大家有所帮助。更多Python学习指路:python基础教程

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4236/viewspace-2830367/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-12

  • 博文量
    76
  • 访问量
    612