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,它会工作的谢谢您指出这一点。。。我应该回去吃饭!