包装器/装饰器-获取其执行的方法的变量的值

问题描述

在包装器/装饰器中,是否有可能在其执行的方法内部获取变量的值?

在此示例中,我想直接在包装器中打印变量 helloThere 的内容。

实际上,我需要直接从包装器中根据此变量执行其他命令(在另一个类中)。

from functools import wraps

class myclass:
    def __init__(self):
        pass

    def _wrapped(f):
        @wraps(f)
        def wrapper(self,*args,**kwargs):
            print(helloThere)
        return wrapper

    @_wrapped
    def doA(self):
        helloThere = "Obi-Wan Kenobi"

    @_wrapped
    def doB(self):
        helloThere = "General Grievous"

提前谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)