How to invoke the super constructor in Python?

后端 未结 7 2385
我寻月下人不归
我寻月下人不归 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:38

    With Python 2.x old-style classes it would be this:

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

提交回复
热议问题