Here __class__ should not be confused with self.__class__ which I am already able to access with the inspect module:
import inspect
class A:
    def __init__(self):
        print(__class__.__name__)  # I want to move this statement inside f
        f()
class B(A):
    pass
def f():
    prev_frame = inspect.currentframe().f_back
    self = prev_frame.f_locals["self"]
    print(self.__class__.__name__)
B()  # prints A B
 
    