在Python 3中-是,您可以*
在参数列表中指定。
从文档:
“ *”或“ * identifier”之后的参数仅是关键字参数,只能使用关键字参数传递。
>>> def foo(pos, *, forcenamed):
... print(pos, forcenamed)
...
>>> foo(pos=10, forcenamed=20)
10 20
>>> foo(10, forcenamed=20)
10 20
>>> foo(10, 20)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: foo() takes exactly 1 positional argument (2 given)
也可以结合使用**kwargs
:
def foo(pos, *, forcenamed, **kwargs):