Python如何获取调用函数(不仅仅是它的名字)?

mar*_*ius 7 python function inspect python-3.x

我想编写一个返回调用函数的函数:

def foo():
    return get_calling_function() #should return 'foo' function object
Run Code Online (Sandbox Code Playgroud)

网上有很多示例如何获取调用函数的名称,但没有如何获取实际对象。我想出了以下获取名称的解决方案,然后在调用函数的全局命名空间中查找它。但是,这不适用于类函数,因为您还需要类名,而且我认为还有许多其他边缘情况。

from inspect import stack
def get_calling_function():
    return stack()[2][0].f_globals[stack()[1][3]]
Run Code Online (Sandbox Code Playgroud)

那么任何建议如何或是否可以编写此函数以使其通用(在 Python 3 上,顺便说一句)?谢谢。

kxr*_*kxr 8

调用可以从任何代码对象(和扩展模块/内置)发生: from exec,execfile从模块名称空间(在导入期间),从类定义内,从方法/类方法/静态方法内,从装饰函数/方法,从嵌套函数中,... - 所以一般没有“调用函数”,并且很难用它做任何好事。

堆栈帧及其代码对象是您可以获得的最通用的对象 - 并检查属性。


这个找到了很多情况下的调用函数:

import sys, inspect

def get_calling_function():
    """finds the calling function in many decent cases."""
    fr = sys._getframe(1)   # inspect.stack()[1][0]
    co = fr.f_code
    for get in (
        lambda:fr.f_globals[co.co_name],
        lambda:getattr(fr.f_locals['self'], co.co_name),
        lambda:getattr(fr.f_locals['cls'], co.co_name),
        lambda:fr.f_back.f_locals[co.co_name], # nested
        lambda:fr.f_back.f_locals['func'],  # decorators
        lambda:fr.f_back.f_locals['meth'],
        lambda:fr.f_back.f_locals['f'],
        ):
        try:
            func = get()
        except (KeyError, AttributeError):
            pass
        else:
            if func.__code__ == co:
                return func
    raise AttributeError("func not found")
     
# Usage

def f():
    def nested_func():
        print get_calling_function()
    print get_calling_function()
    nested_func()

class Y:
    def meth(self, a, b=10, c=11):
        print get_calling_function()
        class Z:
            def methz(self):
                print get_calling_function()
        z = Z()
        z.methz()
        return z
    @classmethod
    def clsmeth(cls):
        print get_calling_function()
    @staticmethod
    def staticmeth():
        print get_calling_function()

f()
y = Y()
z = y.meth(7)
z.methz()
y.clsmeth()
##y.staticmeth()  # would fail
Run Code Online (Sandbox Code Playgroud)

它发现:

<function f at 0x012E5670>
<function nested_func at 0x012E51F0>
<bound method Y.meth of <__main__.Y instance at 0x01E41580>>
<bound method Z.methz of <__main__.Z instance at 0x01E63EE0>>
<bound method Z.methz of <__main__.Z instance at 0x01E63EE0>>
<bound method classobj.clsmeth of <class __main__.Y at 0x01F3CF10>>
Run Code Online (Sandbox Code Playgroud)

但是,它可能找不到函数,或者找到错误的函数,例如,当同一个装饰器用于不同范围内的多个具有相同名称的函数时,会发现错误的装饰器生成的包装器。