python - '类型错误: __init__() got multiple values for argument' with Python 3 using super() function

标签 python python-3.x oop super

我正在使用继承的 Python 3 编写 OOP 程序,当我尝试像这样初始化子类时遇到标题错误:

class Parent:
    def __init__(self, var1, var2):
        self.var1 = var1
        self.var2 = var2

    #more methods that to some stuff

class Child(Parent):
    a = 1 #a and b are class attributes
    b = 2

    def __init__(self, var1 = 1, var2 = 2, var3 = None):
        super().__init__(self, var1 = 1, var2 = 2) #error shows up for this line
        self.var3 = var3

child_obj = Child(var3 = 3)

当我创建 Child 对象时,我收到一条错误消息:TypeError: __init__() gets multiple values for argument 'var1'。有人知道这里可能出了什么问题吗?提前致谢。

最佳答案

请检查您的代码是否有误。 你想要这样的东西吗?

class Parent:
    def __init__(self, var1, var2):
        self.var1 = var1
        self.var2 = var2
        print(var2)

    #more methods that to some stuff

class Child(Parent):
    a = 1 #a and b are class attributes
    b = 2

    def __init__(self, var1 = 1, var2 = 2, var3 = None):
        super().__init__(var1 = 1, var2 = 2) 
        self.var3 = var3

child_obj = Child(var3 = 3)

关于python - '类型错误: __init__() got multiple values for argument' with Python 3 using super() function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60473871/

相关文章:

javascript - 如何将后端(python、flask)与前端(html、css、javascript)连接起来

Python自动化Outlook电子邮件: change sender or default reply-to address

python - 在 pandas groupby 对象中查找和映射重复项

datetime - 在numpy(python)中将日期时间字符串转换为日期时间

c++ - 哪个类负责序列化?

java - 在 Java 中的方法之间使用相同的对象实例

python - 我们可以使用 Spyder 从 Docker 容器远程运行和调试 Python 代码吗

python - 如何将 3 维数组连接到普通数组中?

Python:使用梯形规则快速计算平均值

java - java中的支持和服务方法是什么