如何在Python中调用超级构造函数?

发布于 2021-02-02 23:16:06

class A:
    def __init__(self):
        print("world")

class B(A):
    def __init__(self):
       print("hello")

B()  # output: hello

在所有其他与super构造函数一起使用的语言中,都是隐式调用的。如何在Python中调用它?我希望super(self)这是行不通的。

关注者
0
被浏览
90
1 个回答
  • 面试哥
    面试哥 2021-02-02
    为面试而生,有面试问题,就找面试哥。

    super()在新样式类中返回类似父对象的对象:

    class A(object):
        def __init__(self):
            print("world")
    
    class B(A):
        def __init__(self):
            print("hello")
            super(B, self).__init__()
    
    B()
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看