How to invoke the super constructor in Python?

后端 未结 7 2235
我寻月下人不归
我寻月下人不归 2020-11-22 10:48
class A:
    def __init__(self):
        print(\"world\")

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

B()  # output: hello

In all

7条回答
  •  花落未央
    2020-11-22 11:43

    super() returns a parent-like object in new-style classes:

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

提交回复
热议问题