"TypeError: Person() takes 1 positional argument but 2 were given"但构造函数采用两个参数



这是最奇怪的事情。我的python刚刚停止处理actor中有多个参数的类?运行python 3.8.10获取错误TypeError: Person() takes 1 positional argument but 2 were given

def Person(object):
def __init__(self, a, b):
self.aa = a
self.bb = b
pp = Person(20, 40)

如果我把Person__init__降到一个参数,那么它就工作了。如果我把它提高到3,那么我得到相同的takes 1 but 3 were given错误。我完全被难住了?

您已经声明您的Person类是错误的。您使用了def而不是class,这意味着您实际上有一个名为Person的函数和一个称为__init__的本地函数。

试试这个:

class Person(object):
def __init__(self, a, b):
self.aa = a
self.bb = b

最新更新