How to avoid infinite recursion with super()?

I have code like this:

class A(object):
    def __init__(self):
          self.a = 1

class B(A):
    def __init__(self):
        self.b = 2
        super(self.__class__, self).__init__()

class C(B):
    def __init__(self):
        self.c = 3
        super(self.__class__, self).__init__()

Instantiating B works as expected but instantiating C recursed infinitely and causes a stack overflow. How can I solve this?


When instantiating C calls B.__init__, self.__class__ will still be C, so the super() call brings it back to B.

When calling super(), use the class names directly. So in B, call super(B, self), rather than super(self.__class__, self) (and for good measure, use super(C, self) in C). From Python 3, you can just use super() with no arguments to achieve the same thing


 ? The difference between super().method() versus super(self.__class__,self).method()
 ? How to subclass an OrderedDict?
 ? Django models: managing transactions when commit_manually is deprecated
 ? Django executing backward relation query occurred a maximum recursion depth exceeded error when I use Manager
 ? When running a method from a Python superclass, how can I know the name of the child class that invoked it?
 ? Understanding @property decorator and inheritance
 ? IPython autoreload gives error for repeated calls to Python2 super()
 ? How do I properly inherit from a base class using Super() (Python 2/3)?
 ? How can I interpret this use of Python's super statement
 ? Append is overwriting existing data in list