Get parent class name? [duplicate]

From the documentation: https://docs.python.org/2/reference/datamodel.html#the-standard-type-hierarchy

Class objects have a __name__ attribute. It might cleaner to introspect the base class(es) through the __bases__ attr of the derived class (if the code is to live in the derived class for example).

>>> class Base(object):
...     pass
...
>>> class Derived(Base):
...     def print_base(self):
...         for base in self.__class__.__bases__:
...             print base.__name__
...
>>> foo = Derived()
>>> foo.print_base()
Base

Leave a Comment