有没有办法让装饰者包装的功能? [英] Is there a way to get the function a decorator has wrapped?

查看:131
本文介绍了有没有办法让装饰者包装的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有

Suppose I have

@someDecorator
def func():
   '''this function does something'''
   print 1

现在,对象 func someDecorator 的一个实例。有什么方法可以访问它所持有的函数,即像 func.getInnerFunction()

Now, the object func is an instance of someDecorator. Is there some way I can access the function it holds, i.e something like func.getInnerFunction().

例如,如果我需要检索 func()的文档字符串。

For instance, if I need to retrieve the doc string of func().

推荐答案

请参阅functools.wraps: http://docs.python.org/library/functools。 HTML 。装饰器获取原始函数的名称和文档字符串。你可以这样使用它:

See functools.wraps: http://docs.python.org/library/functools.html. The decorator gets the name and doc string of the original function. You use it like this:

def decorator(f):
    @functools.wraps(f)
    def wrapper():
        ....

这篇关于有没有办法让装饰者包装的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆