这里的super调用了什么?

super用来调用父类方法。

class Student:
    def __init__(self, name, math, chinese):
        self.name = name
        self.math = math
        self.chinese = chinese
    def __getattribute__(self, item):
        print("调用 __getattribute__")
        return super(Student, self).__getattribute__(item)

super指向父类,这里Student的父类object,
如何理解super(Student, self).__getattribute__(item)?
调用object里面的__getattribute__,这个也不好理解?

我感觉这里的super(Student, self)没有指向它的父类,指向了它自身?

回复
阅读 689
1 个回答

https://docs.python.org/zh-cn...

这是官方文档, 注意里面的属性 __mro__, 你再看一下你这个类的 Student.__mro__ 就明白了.

文档上说

For example, if __mro__ of object_or_type is D -> B -> C -> A -> object and the value of type is B, then super() searches C -> A -> object.

在你的示例中, Student.__mro__ 的值是 (Student, object) , 所以搜索的就是 object

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏