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.

see python decorators parameters.


Comments

Popular posts from this blog

java - BeanIO write annotated class to fixedlength -

Using Java 8 lambdas/transformations to combine and flatten two Maps -

How to connect android app to App engine -