Python __init__ *参数 [英] Python __init__ * argument

查看:217
本文介绍了Python __init__ *参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我对Python来说还很陌生,并且有一个我想使用的库.但是,该类的构造函数中有一个参数,我什么也找不到.

So I'm pretty new to Python and there is this library I want to work with. However there is an argument in the constructor of the class which I can't find anything about.

init 方法如下:

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

*的作用是什么?据我所知,自我只是对象本身,而其他只是参数.但是*是什么?

What does the * do? As far as I know the self is just the object itself and the others are just arguments. But what's the * ?

链接到完整的课程: 检查第73行

Link to the full class: check line 73

预先感谢

推荐答案

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

In Python 3, adding * to a function's signature forces calling code to pass every argument defined after the asterisk as a keyword argument:

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

在Python 2中,此语法无效.

In Python 2 this syntax is invalid.

这篇关于Python __init__ *参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆