2

I have a superclass and a subclass. The superclass contains a constructor holding some attributes, and the subclass too, should have a constructor which initializes some attributes. The problem is, however, that when i make an __init__ method in my subclass, it overrides the constructor of the superclass.

How can this be solved so the subclass's constructor doesn't override the superclass's constructor method?

Thank you very much!

stensootla
  • 13,945
  • 9
  • 45
  • 68

1 Answers1

3

Make the subclass call the superclass __init__ method. You can do this either explicitly, or using the super function. For simple cases like single inheritance, both methods are equivalent.

class Subclass(Superclass):
    def __init__(self):
        Superclass.__init__(self) 

class Subclass(Superclass):
    def __init__(self):
        super(Subclass, self).__init__()
Antimony
  • 37,781
  • 10
  • 100
  • 107