python's decorator&wrapper

[python's decorator&wrapper]

decorator

  A function returning another function, usually applied as a function transformation using the @wrapper syntax. Common examples for decorators are classmethod() andstaticmethod().

  The decorator syntax is merely syntactic sugar, the following two function definitions are semantically equivalent:

  python's decorator&wrapper_第1张图片

  The same concept exists for classes, but is less commonly used there.

udpate_wrapper: 把wrapped的属性值拷贝到wrapper

  python's decorator&wrapper_第2张图片

wraps: partial of udpae_wrapper

  python's decorator&wrapper_第3张图片

 how to use ?

 python's decorator&wrapper_第4张图片 

参考:  http://docs.python.org/2.7/library/functools.html#functools.update_wrapper

你可能感兴趣的:(Decorator)