Can the parent and child class be same in python inheritance?

Viewed 81

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__()
1 Answers

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.

Related