get current function name inside that function using python

前端 未结 4 1038
囚心锁ツ
囚心锁ツ 2021-02-01 07:12

For my logging purpose i want to log all the names of functions where my code is going

Does not matter who is calling the function , i want the the function name in whic

4条回答
  •  长发绾君心
    2021-02-01 08:05

    You probably want inspect.getframeinfo(frame).function:

    import inspect
    
    def whoami(): 
        frame = inspect.currentframe()
        return inspect.getframeinfo(frame).function
    
    def foo():
        print(whoami())
    
    foo()
    

    prints

    whoami
    

提交回复
热议问题