Python Decorators I encounter a Strange error -
my code simple. it's test, big error :nonetype 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() but when modified,the error removed:
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() can tell reason? totally confused.
morever, in second code block, how can methon wrapper() visit variable ‘fun’,the variable ‘fun’ not in context(the arg of upper methond 'when' instead of 'fun'), confused too.
thanks help
when decorator provided arguments, treated 'decorator generator', , must return proper decorator.
Comments
Post a Comment