decorator是python的一个特性。
decorator可以是函数也可以是一个类。
decorator@的用处:
1)在函数开始前做一些初始工作,例如参数校验,调用统计等。
2)在函数结束后做一些收尾工作。
3)类似C++模板的功能。
decorator是函数时,
def decorator(fun): def call(*args, **kwargs): print "do something before fun!" if len(args) < 1: print "args is less" else: fun(args[0]) print "do something after fun!" return call @decorator def hello(a): print "hello" # hello(*args, **kwargs) # is equivalent to # decorator(hello)(*args, **kwargs) # see the example for more detail print "hello() is called:" hello() print "-----------------------------" print "hello(1) is called:" hello(1) # decorator chain def chain0(fun): def call(*args0, **kwargs0): print "chain0" fun(*args0, **kwargs0) return call def chain1(fun): def call(*args1, **kwargs1): print "chain1" fun(*args1, **kwargs1) return call @chain0 @chain1 def chain2(*args2, **kwargs2): print "chain2" # attention!! # chain2(*args0, **kwargs0) # is equivalent to chain0(chain1(chain2))(*args0, **kwargs0) # it is not chain2(*args2, **kwargs2) # it is not chain0(chain1(chain2(*args0, **kwargs0))) # it is not chain0(chain1(chain2(*args2, **kwargs2))) chain2()
运行结果:
hello() is called:
do something before fun!
args is less
do something after fun!
-----------------------------
hello(1) is called:
do something before fun!
hello
do something after fun!
chain0
chain1
chain2
具体原因看注释就明白了。
decorator是类时,
class decorator(object): def __init__(self, func): print func self.func = func print '__init__' def __call__(self, *args, **kwargs): print 'obj = %s, cls = %s' % (self.obj, self.cls) return self.func.__call__(*args, **kwargs) def __get__(self, obj, cls): self.obj = obj self.cls = cls print '__get__' return self.__call__ class foo(object): @decorator def hello(self): print 'hello' @decorator def world(self): print 'world' tmp = foo() print '-------------------------' tmp.hello(tmp) tmp.world(tmp)
运行结果:
<function hello at 0x2b978ba2d578>
__init__
<function world at 0x2b978ba2d5f0>
__init__
-------------------------
__get__
obj = <__main__.foo object at 0x2b978f03a710>, cls = <class '__main__.foo'>
hello
__get__
obj = <__main__.foo object at 0x2b978f03a710>, cls = <class '__main__.foo'>
world
可以看出在创建foo实例的时候,
foo中的函数如果有对应的类decorator,
那么类decorator也会分别做相应的初始化。
__get__之后返回一个函数,然后就跟前面的函数decorator一样了。
相关推荐
资源分类:Python库 所属语言:Python 资源全名:drf_nested_decorator-0.3-py2-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
超时装饰器安装从源代码: python setup.py install从pypi: pip install timeout-decorator用法import timeimport timeout_decorator@timeout_decorator.timeout(5)def mytest(): print("Start") for i in range(1,...
odoo包
软件介绍: 这个decorator python是网友分享过来的一个模块文件,既然下载肯定会知道它的用途。
本文研究的主要是python实现Decorator模式,具体介绍如下。 一般来说,装饰器是一个函数,接受一个函数(或者类)作为参数,返回值也是也是一个函数(或者类)。首先来看一个简单的例子: # -*- coding: utf-8 -*- ...
主要介绍了Python decorator拦截器代码实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
本文实例讲述了Python装饰器decorator用法。分享给大家供大家参考。具体分析如下: 1. 闭包(closure) 闭包是Python所支持的一种特性,它让在非global scope定义的函数可以引用其外围空间中的变量,这些外围空间中...
python中的decorator通常为输入一个函数,经过装饰后返回另一个函数。 比较常用的功能一般使用decorator来实现,例如python自带的staticmethod和classmethod。 装饰器有两种形式: 复制代码 代码如下: @A def foo()...
在编程过程中,经常遇到这样的场景:登录校验,权限校验,日志记录等,这些功能代码在各个环节都可能需要,但又十分雷同,通过装饰器来抽象、剥离这部分代码可以很好解决这类场景,这篇文章主要介绍了Python的装饰器...
离线安装包,亲测可用
自动主 使用语法的更简洁的快捷方式: def mymain(): print 'this is our main... from automain import * # will only import the automain decorator @automain def mymain(): print 'this is our main function'
python-decorator-3.0.1-3.1.el6.noarch
主要介绍了Python中设计模式之Decorator装饰器模式模式,文中详细地讲解了装饰对象的相关加锁问题,需要的朋友可以参考下
资源分类:Python库 所属语言:Python 资源全名:cache_decorator-1.2.0.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
python中完善decorator共5页.pdf.zip
python中编写带参数decorator共5页.pdf.zip
python中编写无参数decorator共4页.pdf.zip
demo python使用装饰器(Decorator)的方式实现单例模式 functools.wraps 则可以将原函数对象的指定属性复制给包装函数对象, 默认有 __module__、__name__、__doc__,或者通过参数选择