即使我们没有传递任何参数,子 class 中 Super() 的用途是什么

what is the Use of Super() in child class even we are not passing any argument

    class Base(object):
        def __init__(self):
           self.fname="MS"
           self.lname="Dhoni"
    
    class Child(Base):
       def __init__(self):
           self.fname="kohli"
           super(Base).__init__()

上面代码中 super 方法的用途是什么,甚至评论 super(Base).__init__() 我得到输出 kohli 请解释

您正在呼叫 super(Base),这意味着 Baseclass 的 parent object class,所以你不是调用Base.__init__方法,这意味着没有 re-assignment of fname 留在 kohli


你想要的是 parent of Child class,当前实例 self

super(Child, self).__init__()

但其实你只要做下面的就可以了,都是一样的

super().__init__()