python语法32[装饰器decorator]



一 装饰器decorator 

decorator设计模式允许动态地对现有的对象或函数包装以至于修改现有的职责和行为,简单地讲用来动态地扩展现有的功能。其实也就是其他语言中的AOP的概念,将对象或函数的真正功能也其他辅助的功能的分离。
 

二Python中的decorator

python中的decorator通常为输入一个函数,经过装饰后返回另一个函数。  比较常用的功能一般使用decorator来实现,例如python自带的staticmethod和classmethod。

 

 

装饰器有两种形式:

@A
def foo():
     pass

 

相当于:

def foo():
     pass
foo = A(foo)

 

第二种为带参数的:

@A(arg)
def foo():
     pass

 

则相当于:

def foo():
     pass
foo = A(arg)(foo)

 

可以看出第一种的装饰器是个返回函数的函数,第二种的装饰器是个返回函数的函数的函数。 

 

python中的decorator可以多个同时使用,如下:

 

@A
@B
@C
def f ():  pass
    
#  it is same as below
def f():  pass
f = A(B(C(f)))

 


三 Python中常用的decorator实例

decorator通常用来在执行前进行权限认证,日志记录,甚至修改传入参数,或者在执行后对返回结果进行预处理,甚至可以截断函数的执行等等。


实例1:

from functools  import wraps
def logged(func):
    @wraps(func)
     def with_logging(*args, **kwargs):
         print (func. __name__() +  "  was called ")
         return func(*args, **kwargs)
     return with_logging

@logged
def f(x):
    """ does some math """
    return x + x * x

print (f. __name__)   #  prints 'f'
print (f. __doc__)    #  prints 'does some math'

 

注意functools.wraps()函数的作用:调用经过装饰的函数,相当于调用一个新函数,那查看函数参数,注释,甚至函数名的时候,就只能看到装饰器的相关信息,被包装函数的信息被丢掉了。而wraps则可以帮你转移这些信息,参见http://stackoverflow.com/questions/308999/what-does-functools-wraps-do 

 

参考: 
http://www.cnblogs.com/Lifehacker/archive/2011/12/20/3_useful_python_decorator.html#2277951 

 

完! 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM