python代码反馈错误

_ init _()take 1 positional argument but 4 were given这是什么错误?

这个错误是说__init__()中只设置了1个参数的位置,但调用时却传了4个参数
比如

class Aclass:
    def __init__(self):  #__init__()中只设置了1个参数self的位置
        pass

a=Aclass(1,2,3) #,但调用时却传了1,2,3 加上对象自动传递的self,  4个参数

如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img

准确来说,这个是因为__init__所接收的位置参数(positional argument)仅有self一个,而传入了4个位置参数
__init__只是没有别的位置参数,仍可能有关键字参数

class Simple:
    def __init__(self, *, key = True):
        pass

s = Simple('a', 'b', 'c')   # TypeError: __init__() takes 1 positional argument but 4 were given

s = Simple(key = False)     # 正常创建

img