1

In python inheritance, we can usually inherit parent properties to the child class. However, I do not get the idea of inheriting within the same class. What does this mean?

class MyParentClass():
    def __init__(self):
        super(MyParentClass, self).__init__()
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Lakpa Tamang
  • 408
  • 1
  • 4
  • 12
  • 1
    If’s you are referring to super, that is *not* inheriting from the class it refers to. – MisterMiyagi Sep 07 '22 at 06:12
  • 1
    Nothing, I do not get why you will want to inheritate from same class. it already has all attributes an methods. Besides in your code there is no inheritance. – Lucas M. Uriarte Sep 07 '22 at 06:15
  • Does this answer your question? [Python super() inheritance and needed arguments](https://stackoverflow.com/questions/15896265/python-super-inheritance-and-needed-arguments) – MisterMiyagi Sep 07 '22 at 06:57

1 Answers1

5

Every class in python 2.6 and above inherits from object unless explicitly stated otherwise, so class MyParentClass(): is equivalent to class MyParentClass(object). Under normal circumstances, the super call is accessing object.__init__.

Under less normal circumstances, MyParentClass may be a mixin. This is a class that participates in multiple inheritance in a way that adds functionality to the child without being part of the main inheritance chain. I this case, super is referring to some unknown base class in the MRO. The reason that you would want to call the unknown __init__ method in this case is to continue the chain of __init__ calls all the way to the base class.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • @Alexander. I don't see that this is an obvious duplicate. Certainly not to the question you linked, although there is some common ground. – Mad Physicist Sep 07 '22 at 06:18
  • 3
    I don't think the answer is useful as worded right now. It *seems* (yes, there's also the problem that the question is not exactly clear) that the OP is confused about `super` referring to the same class, which this answer does not address at all. Rather, it refers to all sorts of trivia of `super` that are not wrong but unlikely to help with the initial confusion. – MisterMiyagi Sep 07 '22 at 06:52