您的当前位置:首页正文

python可变参数的使用注意

来源:华拓网

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学习指路:

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