EDIT: As Alex noted, this leads to infinite recursion when there is more than one level of inheritance. Do not use this approach.
Yes, the βnewβ style classes have the __class__ attribute, which can be used, for example.
class B(object): def __init__(self): print "B.__init__():" class D(B): def __init__(self): print "D.__init__():" super(self.__class__, self).__init__() >>> d = D() D.__init__(): B.__init__(): >>> dir(d) ['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__'] >>> d.__class__ <class '__main__.D'>
However, this fails if the class must inherit from D :
>>> class E(D): ... pass ... >>> E() D.__init__(): D.__init__(): [...] D.__init__(): D.__init__(): Traceback (most recent call last): File "<stdin>", line 4, in __init__ File "<stdin>", line 4, in __init__ [...] File "<stdin>", line 4, in __init__ File "<stdin>", line 4, in __init__ RuntimeError: maximum recursion depth exceeded while calling a Python object
source share