Python __init__ * 参数

rie*_*age 5 python init

我对 Python 还很陌生,我想使用这个库。但是,该类的构造函数中有一个参数,我找不到任何相关信息。

init方法如下所示:

def __init__(self, ain1, ain2, bin1, bin2, *, microsteps=16):

* 有什么作用?据我所知,自我只是对象本身,其他只是论证。但是 * 是什么?

完整课程链接: 查看第 73 行

提前致谢

Dee*_*ace 10

在 Python 3 中,添加*到函数的签名会强制调用代码将星号后面定义的每个参数作为关键字参数传递:

>> def foo(a, *, b):
..     print('a', a, 'b', b)

>> foo(1, 2)
TypeError: foo() takes 1 positional argument but 2 were given

>> foo(1, b=2)
a 1 b 2
Run Code Online (Sandbox Code Playgroud)

在 Python 2 中此语法无效。