Python 重复使用装饰过的功能

Python 重复使用装饰过的功能,python,python-decorators,Python,Python Decorators,这个问题是关于在外部范围内重用修饰函数的 如果我能让下面的代码正常工作,那就太棒了。我知道函数作为一个参数传递给eat,但是有没有办法让apple在范围内被识别 class A: def eat( s, func ): print 123 def __init__(s): @s.eat def apple(): print 456 apple() # "apple" is not defined ... 谢谢 诚恳地说,你的意思是像原来

这个问题是关于在外部范围内重用修饰函数的

如果我能让下面的代码正常工作,那就太棒了。我知道函数作为一个参数传递给
eat
,但是有没有办法让
apple
在范围内被识别

class A:

  def eat( s, func ):
    print 123

  def __init__(s):
    @s.eat
    def apple():
      print 456

    apple() # "apple" is not defined ...
谢谢


诚恳地说,

你的意思是像原来的
苹果
?你是什么意思
苹果
不被认可?您将遇到的错误是,
apple
不再可调用,因为您的装饰程序返回
None
,并且
apple
现在已设置为None。i、 e.
TypeError:“NoneType”对象不可调用
唯一的另一种解释是如果代码缩进不正确…如果您修复装饰器并让它
返回func
@Rawing,它会工作的谢谢您指出这一点。。。我应该回去吃饭!