1. Only the name of the function is taken
1 ImportSYS2 defgetfunctionname ():3 returnSys._getframe (1). F_code.co_name4 5 defrun ():6 Print 'functionname:', Getfunctionname ()7 8 if __name__=="__main__":9Run ()
2. Take class name, function name
1 ImportInspect2 defget_current_function_name ():3 returnInspect.stack () [1][3]4 5 classMyClass:6 defFunction_one (self):7 Print "%s.%s invoked"% (self.__class__.__name__, Get_current_function_name ())8 9 if __name__=="__main__":TenMyClass =MyClass () OneMyclass.function_one ()
Or
1 ImportSYS2 defgetfunctionname ():3 returnSys._getframe (1). F_code.co_name4 5 classMyClass:6 defFunction_one (self):7 Print "%s.%s invoked"% (self.__class__.__name__, Getfunctionname ())8 9 if __name__=="__main__":TenMyClass =MyClass () OneMyclass.function_one ()
Dynamically gets the currently running class name, function name