Get parent function

后端 未结 4 431
不思量自难忘°
不思量自难忘° 2021-01-02 00:39

Is there a way to find what function called the current function? So for example:

def first():
    second()

def second():
    # print out here what function         


        
4条回答
  •  天涯浪人
    2021-01-02 01:05

    import inspect
    
    def first():
        return second()
    
    def second():
        return inspect.getouterframes( inspect.currentframe() )[1]
    
    first()[3] # 'first'
    

提交回复
热议问题