python装饰器

def w1(func):
    def inner(*args,**kwargs):
        # you code
        return func(*args,**kwargs)
    return inner
 
@w1
def f1(arg1,arg2,arg3):
    print 'f1' 

以上代码理解:(w1(f1))(arg1,arg2,arg3)

def w1(func):
    def inner(*args,**kwargs):
        # you code
        return func(*args,**kwargs)
    return inner
 
def w2(func):
    def inner(*args,**kwargs):
        # you code
        return func(*args,**kwargs)
    return inner
 
 
@w1
@w2
def f1(arg1,arg2,arg3):
    print 'f1'

以上代码理解:(w1(w2(f1)))(arg1,arg2,arg3)

def Before(request,kargs):
    print 'before'
      
def After(request,kargs):
    print 'after'
  
  
def Filter(before_func,after_func):
    def outer(main_func):
        def wrapper(request,kargs):
              
            before_result = before_func(request,kargs)
            if(before_result != None):
                return before_result;
              
            main_result = main_func(request,kargs)
            if(main_result != None):
                return main_result;
              
            after_result = after_func(request,kargs)
            if(after_result != None):
                return after_result;
              
        return wrapper
    return outer
      
@Filter(Before, After)
def Index(request,kargs):
    print 'index'
以上代码理解:(Filter(Before, After)(Index))(request,kargs)


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页