重复使用装饰功能

2024-09-23 04:23:58 发布

您现在位置:Python中文网/ 问答频道 /正文

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

如果我能让下面的代码正常工作,那就太棒了。我知道函数是作为参数传递给eat的,但是有没有办法保持apple在作用域中被识别?你知道吗

class A:

  def eat( s, func ):
    print 123

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

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

谢谢!你知道吗

真诚地


Tags: 函数代码appleinitisdefnot作用域