您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

星号(*)作为python函数的自变量

星号(*)作为python函数的自变量

*指示的位置参数的结尾。此后的每个参数只能由关键字指定。这在PEP 3102中定义

>>> def foo1(a, b=None):
...     print(a, b)
...
>>> def foo2(a, *, b=None):
...     print(a, b)
...
>>> foo1(1, 2)
1 2
>>> foo2(1, 2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: foo1() takes 1 positional argument but 2 were given
>>> foo2(1, b=2)
1 2
python 2022/1/1 18:41:02 有456人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶