通过字符串名称获取修饰的函数对象 [英] Get decorated function object by string name

查看:73
本文介绍了通过字符串名称获取修饰的函数对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

def log(func):
    def wraper(*a, **kw):   
        return func(*a, **kw)
    return wraper

@log
def f():
    print 'f'


print locals()['f'] # - prints <function wraper at 0x00CBF3F0>.

如何获得真正的f对象(不是装饰器包装)?

How do you get the real f object (not decorator wrap)?

推荐答案

您不需要。 1 将其存储起来,以备日后使用。

You don't.1 Store it if you need to access it later.

def log(func):
  def wrapper(*a, **kw):
    return func(*a, **kw)
  wrapper.func = func
  return wrapper

@log
def f():
  print 'f'

print f.func

1 ,但我不推荐。

1 You could mess with the closure, but I can't recommend it.

这篇关于通过字符串名称获取修饰的函数对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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