Python装饰程序我遇到了一个奇怪的错误

Python装饰程序我遇到了一个奇怪的错误,python,python-decorators,Python,Python Decorators,我的代码非常简单。这是一个测试,但我得到一个大错误:nonetype不可调用: def deco(when): def wrapper(fun): print 'before call myfun' fun() print 'after call myfun' return wrapper @deco('a') def fun(): print 'in fun1' fun() 但是,如果稍微修改,则会删除错误: def

我的代码非常简单。这是一个测试,但我得到一个大错误:nonetype不可调用:

def deco(when):
    def wrapper(fun):
        print 'before call myfun'
        fun()
        print 'after call myfun' 
    return wrapper 
@deco('a')
def fun():
    print 'in fun1'
fun()
但是,如果稍微修改,则会删除错误:

def deco(when):
    def wrapper(fun):
        def dec():
            print 'before call myfun'
            fun()
            print 'after call myfun'
        return dec
    return wrapper

@deco('a')
def fun():
    print 'in fun'
fun()
你能说出原因吗?我完全糊涂了

此外,在第二个代码块中,methon wrapper()如何访问变量'fun',变量'fun'不在上下文中(上层methond的arg是'when'而不是'fun'),我也感到困惑

感谢您的帮助

装饰器是一种接受函数并返回新函数的函数。它通常看起来像

def deco(fun):
    def wrapped(*args, **kwargs):
        # something... 
        return fun(*args, **kwargs)
    return wrapped
@
之后的表达式必须返回修饰符。如果该表达式是一个函数调用,那么它调用的函数必须返回一个decorator。因此,它必须看起来像

def deco_with_args(arg):
    def deco(fun): # The decorator function that will be returned
        def wrapped(*args, **kwargs): # The wrapper function that will finally be assigned to the decorated name
            # something... 
            return fun(*args, **kwargs)
        return wrapped
     return deco
decorator是接受函数并返回新函数的函数。它通常看起来像

def deco(fun):
    def wrapped(*args, **kwargs):
        # something... 
        return fun(*args, **kwargs)
    return wrapped
@
之后的表达式必须返回修饰符。如果该表达式是一个函数调用,那么它调用的函数必须返回一个decorator。因此,它必须看起来像

def deco_with_args(arg):
    def deco(fun): # The decorator function that will be returned
        def wrapped(*args, **kwargs): # The wrapper function that will finally be assigned to the decorated name
            # something... 
            return fun(*args, **kwargs)
        return wrapped
     return deco
decorator是接受函数并返回新函数的函数。它通常看起来像

def deco(fun):
    def wrapped(*args, **kwargs):
        # something... 
        return fun(*args, **kwargs)
    return wrapped
@
之后的表达式必须返回修饰符。如果该表达式是一个函数调用,那么它调用的函数必须返回一个decorator。因此,它必须看起来像

def deco_with_args(arg):
    def deco(fun): # The decorator function that will be returned
        def wrapped(*args, **kwargs): # The wrapper function that will finally be assigned to the decorated name
            # something... 
            return fun(*args, **kwargs)
        return wrapped
     return deco
decorator是接受函数并返回新函数的函数。它通常看起来像

def deco(fun):
    def wrapped(*args, **kwargs):
        # something... 
        return fun(*args, **kwargs)
    return wrapped
@
之后的表达式必须返回修饰符。如果该表达式是一个函数调用,那么它调用的函数必须返回一个decorator。因此,它必须看起来像

def deco_with_args(arg):
    def deco(fun): # The decorator function that will be returned
        def wrapped(*args, **kwargs): # The wrapper function that will finally be assigned to the decorated name
            # something... 
            return fun(*args, **kwargs)
        return wrapped
     return deco

当为装饰器提供参数时,它将被视为“装饰器生成器”,并且必须返回正确的装饰器


另请参见。

当为装饰器提供参数时,它将被视为“装饰器生成器”,并且必须返回正确的装饰器


另请参见。

当为装饰器提供参数时,它将被视为“装饰器生成器”,并且必须返回正确的装饰器


另请参见。

当为装饰器提供参数时,它将被视为“装饰器生成器”,并且必须返回正确的装饰器


另请参见。

请修正缩进。请修正缩进。请修正缩进。请修正缩进。装饰生成器的概念非常生动,感谢您的链接,它解决了我的问题。装饰生成器的概念非常生动,感谢您的链接,它解决了我的问题“装饰发电机”的概念非常生动,谢谢你的链接,它解决了我的问题“装饰发电机”的概念非常生动,谢谢你的链接,它解决了我的问题