Child class name in parent class function at run time Python

python

Solution

You'd use: `self.__class__.__name__` to retrieve the name of the class of the instance `self`.

class ParentClass:

    def print_child_class_name(self):
        print(self.__class__.__name__)

class ChildClass1(ParentClass):
    pass

class ChildClass2(ParentClass):
   pass

child_1 = ChildClass1()
child_2 = ChildClass2()
child_1.print_child_class_name() #Prints ChildClass1
child_2.print_child_class_name() # Prints ChildClass2

Problem

Is it possible to find out which child class has called the parent class function in the parent class function ``` class ParentClass: def print_child_class_name(self): print('how to find print the child class name here') class ChildClass1(ParentClass): pass class ChildClass2(ParentClass): pass child_1 = ChildClass1() child_2 = ChildClass2() child_1.print_child_class_name() child_2.print_child_class_name() ```

Original source