get current function name inside that function using python

前端 未结 4 1086
囚心锁ツ
囚心锁ツ 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:14

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

    Have you considered decorators?

    import functools
    def logme(f):
        @functools.wraps(f)
        def wrapped(*args, **kwargs):
            print(f.__name__)
            return f(*args, **kwargs)
        return wrapped
    
    
    @logme
    def myfunction():
        print("Doing some stuff")
    

提交回复
热议问题