This is probably the fastest implementation. It avoids the use of additional inspect
methods which can be slow or unnecessary.
Implementation:
from inspect import currentframe
def get_self_name() -> str:
return currentframe().f_code.co_name
def get_caller_name() -> str:
return currentframe().f_back.f_code.co_name
def get_parent_caller_name() -> str:
return currentframe().f_back.f_back.f_code.co_name
Usage:
def actual_function_1():
print('In actual_function_1:', get_self_name())
def actual_function_2():
print('In actual_function_2:', get_caller_name())
def actual_function_3() -> None:
print('In actual_function_3:', get_parent_caller_name())
actual_function_1()
actual_function_2()
actual_function_3()
Output:
In actual_function_1: get_self_name
In actual_function_2: actual_function_2
In actual_function_3: <module>