栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Python如何获取调用函数(不仅仅是其名称)?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python如何获取调用函数(不仅仅是其名称)?

可以从任何代码对象(和扩展模块/内置)调用:从

exec
execfile
在模块名称空间(在导入期间),在类定义内,在方法/类方法/静态方法内,从修饰的函数/方法内,从嵌套函数中…,因此通常没有“调用函数”,并且很难做到这一点。

堆栈框架及其代码对象是最常用的-检查属性。


在许多情况下,这可以找到调用函数:

import sys, inspectdef get_calling_function():    """finds the calling function in many decent cases."""    fr = sys._getframe(1)   # inspect.stack()[1][0]    co = fr.f_pre    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.__pre__ == co:     return func    raise AttributeError("func not found")# Usagedef 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

它发现:

<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>>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/374440.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号