super()函数调用父类方法一直报错,代码全程按照教程来的

按照教程写类继承的代码,但是一直结果报错,代码如下 比较简单

class father(object):
    def __init__(self, attribute_value1, attribute_value2):
        self.attribute1 = attribute_value1
        self.attribute2 = attribute_value2

    def __str__(self):
        return "\"父类father\""

    def __del__(self):
        return "已删除%s" % (father.__str__(self))

    def testFunction(self, parameter1, parameter2):
        return f"{father.__str__(self)}的属性值是{self.attribute1}{self.attribute2},参数是{parameter1}{parameter2}"


class son(father):
    def __init__(self):
        super(father, self).__init__()
        self.attribute1, self.attribute2 = "1", 2

    def __str__(self):
        return "子类son"

    def __del__(self):
        return "已删除%s" % (son.__str__(self))

    def old_method(self):
        super(father, self).__init__()
        return super(father, self).testFunction()


result = son()
print(result.attribute2, result.attribute1)
print(result.old_method())


然后运行下来是报错,就是这个错误"AttributeError: 'super' object has no attribute '"

img

我检查了好几遍缩进的情况,也在网上查了,没找到解决办法

请问有没有办法能解决啊 感谢各位

super(father, self)
改为
super(son, self)

结论:第 29 行写错了,把第 27 到 29 行的函数改成:

    def old_method(self):
        return self.testFunction(self.attribute1, self.attribute2)

分析:super 函数是用来初始化父类的,在 init 方法中使用一次就行了,调用的时候使用 self 调用继承父类的方法。如果解决了你的问题,请给个采纳谢谢。

报错原因是你调用的方法入参格式不符合要求,增加参数即可

img