How to invoke the super constructor?

2019-01-01 05:14发布

问题:

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

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

B()
hello

In all other languages I\'ve worked with the super constructor is invoked implicitly. How does one invoke it in Python? I would expect super(self) but this doesn\'t work.

回答1:

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()


回答2:

In line with the other answers, there are multiple ways to call super class methods (including the constructor), however in Python-3.x the process has been simplified:

Python-2.x

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

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

Python-3.x

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

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

super() is now equivalent to super(<containing classname>, self) as per the docs.



回答3:

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)


回答4:

One way is to call A\'s constructor and pass self as an argument, like so:

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

The advantage of this style is that it\'s very clear. It call A\'s constructor. The downside is that it doesn\'t handle diamond-shaped inheritance very well, since you may end up calling the shared base class\'s constructor twice.

Another way is to use super(), as others have shown. For single-inheritance, it does basically the same thing as letting you call the parent\'s constructor.

However, super() is quite a bit more complicated under-the-hood and can sometimes be counter-intuitive in multiple inheritance situations. On the plus side, super() can be used to handle diamond-shaped inheritance. If you want to know the nitty-gritty of what super() does, the best explanation I\'ve found for how super() works is here (though I\'m not necessarily endorsing that article\'s opinions).



回答5:

I use the following formula that extends previous answers:

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

class B(A):
 def __init__(self):
   print \"hello\"
   super(self.__class__, self).__init__()

B()

This way you don\'t have to repeat the name of the class in the call to super. It can come handy if you are coding a large number of classes, and want to make your code in the constructor methods independent of the class name.



回答6:

Short Answer

super(DerivedClass, self).__init__()

Long Answer

What does super() do?

It takes specified class name, finds its base classes (Python allows multiple inheritance) and looks for the method (__init__ in this case) in each of them from left to right. As soon as it finds method available, it will call it and end the search.

How do I call init of all base classes?

Above works if you have only one base class. But Python does allow multiple inheritance and you might want to make sure all base classes are initialized properly. To do that, you should have each base class call init:

class Base1:
  def __init__():
    super(Base1, self).__init__()

class Base2:
  def __init__():
    super(Base2, self).__init__()

class Derived(Base1, Base2):
  def __init__():
    super(Derived, self).__init__()

What if I forget to call init for super?

The __init__ for base class is not called. There is no implicit constructor calls like in C++ and Java.