Python 装饰器我遇到了一个奇怪的错误
Python Decorators I encounter a Strange error
我的代码很简单。这是为了测试,但我得到了一个很大的错误:nonetype is not callable:
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'不在上下文中(上面方法的arg 是'when' 'fun'), 我也很困惑。
感谢您的帮助
一个装饰器是一个接受一个函数和return一个新函数的函数。它通常看起来像
def deco(fun):
def wrapped(*args, **kwargs):
# something...
return fun(*args, **kwargs)
return wrapped
@
之后的表达式必须 return 装饰器。如果该表达式是一个函数调用,那么它调用的函数必须 return 一个装饰器。因此,它必须看起来像
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 generator',并且必须 return 一个合适的装饰器。
另见 python decorators with parameters。
我的代码很简单。这是为了测试,但我得到了一个很大的错误:nonetype is not callable:
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'不在上下文中(上面方法的arg 是'when' 'fun'), 我也很困惑。
感谢您的帮助
一个装饰器是一个接受一个函数和return一个新函数的函数。它通常看起来像
def deco(fun):
def wrapped(*args, **kwargs):
# something...
return fun(*args, **kwargs)
return wrapped
@
之后的表达式必须 return 装饰器。如果该表达式是一个函数调用,那么它调用的函数必须 return 一个装饰器。因此,它必须看起来像
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 generator',并且必须 return 一个合适的装饰器。
另见 python decorators with parameters。