class A(object):
def get_class(self):
return self.__class__
class B(A):
def __init__(self):
A.__init__(self)
b = B()
print b.get_class()
This code will print <class '__main__.B'>.
How can I get the class name where the method has been defined (namely A)?
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).