一、内建装饰器
包括property,classmethod,staticmethod
classmethod与s taticmethod
class deco_test: @staticmethod def test_static(): print 'class test static' def test_normal(self): print "normal" @classmethod def test_class(cls): print "class",cls def test_static_method(): print "global test static" deco=deco_test() test_static_method() deco_test.test_static() deco.test_static() deco.test_normal() deco.test_class() print deco_test print deco
结果:
global test static class test static class test static normal class __main__.deco_test __main__.deco_test <__main__.deco_test instance at 0x7f46e46c63f8>
staticmethod装饰器,类实例和对象都可以访问,和类中的普通方法相比,不带self参数,可以用全局方法代替
classmethod将类自己作为对象传给class方法,
>>> class A: ... @classmethod ... def test_class(cls): ... return cls ... >>> a=A() >>> a.test_class() <class __main__.A at 0x7ff898f7e188> >>> A <class __main__.A at 0x7ff898f7e188>
property:使用方法
class rabbit(object): def __init__(self,name): self._name=name #set read attrib @property def name(self): return self._name #set write attrib @name.setter def name(self,name): self._name=name #set del attrib @name.deleter def name(self): del self._name ra=rabbit("kyle") print ra.name #write ra.name="kate" print ra.name #del del ra.name try: print ra.name except AttributeError ,e: print e
结果
kyle kate 'rabbit' object has no attribute '_name'
使用property后,方法就变成了类的属性,默认情况下是有读属性,如果要有写和删除的话,需按@functionname.setter,@functionname.deleter设置
二、自定义装饰器
首先说明函数在python中也是对象,也能作为函数返回值,先理解这点,装饰器就好明白了。
def deco1(func): def warpper(*args,**kwargs): print "before" func(*args,**kwargs) print "after" return warpper @deco1 def foo(x): print x foo(1)
结果:
before 1 after
如果还不是很明白,换一种写法就明白了
def deco1(func): def warpper(*args,**kwargs): print "before" func(*args,**kwargs) print "after" return warpper def foo(x): print x foo=deco1(foo) foo(1)
返回结果同上面一样,其实可以理解为被装饰的函数对象传递给装饰函数,并返回同被装饰函数相同名字的函数对象
多重装饰
def deco1(func): def warpper(*args,**kwargs): print "before" func(*args,**kwargs) print "after" return warpper def deco2(func): def wraper(*args,**kwargs): print "yes" func(*args,**kwargs) print "no" return wraper @deco1 @deco2 def foo1(x,y): print (x,y) foo1(1,2)
结果:
before yes (1, 2) no after
多重装饰的话注意顺序就ok
相关推荐
!!!CSDN的一个特性: 即使我...关于Python装饰器课程的课件代码相关文件, 文字版讲义见: https://blog.csdn.net/ncepu_Chen/article/details/106075394 视频版本讲义见: https://www.bilibili.com/video/BV1SZ4y1s7cv/
今天小编就为大家分享一篇关于Python装饰器限制函数运行时间超时则退出执行,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
python装饰器详解,python装饰器笔记心得
本工程是使用demo构建装饰器,以及装饰器的使用案例。 从最简单的装饰器到带参数的,带任意参数的,到类装饰器等。代码案例比较全。可作为学习参考与交流。
Python装饰器-失败用例自动重试
python装饰器是一个用于封装函数、方法或类的代码的工具,用来显式指定管理它们的代码。一次编写,可用于多种不同的情况。在python 的流行框架中,装饰器应用越来越广泛。用类设计装饰器,可以利用实例属性保持装饰...
Python装饰器decoder.py 装饰模式有很多经典的使用场景,例如插入日志、性能测试、事务处理等等,有了装饰器, 就可以提取大量函数中与本身功能无关的类似代码,从而达到代码重用的目的。下面就一 步步看看Python中...
python 装饰器(示例)
装饰器(decorator)是一种高级Python语法。装饰器可以对一个函数、方法或者类进行加工。在Python中,我们有多种方法对函数...Python装饰器以两种形式呈现: 【1】函数装饰器在函数定义的时候进行名称重绑定,提供一个逻
python 装饰器概述装饰器本质上是一个Python函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外功能,装饰器的返回值也是一个函数对象。代码de
接口测试使用Python装饰器.doc
Python36_01----python装饰器
本文实例讲述了Python装饰器用法。分享给大家供大家参考,具体如下: 写装饰器 装饰器只不过是一种函数,接收被装饰的可调用对象作为它的唯一参数,然后返回一个可调用对象(就像前面的简单例子) 注意重要的一点,...
异步属性的Python装饰器。 的Python:3.6+ 免费软件:MIT许可证 说明文件: : 包: : 源代码: : 安装 要安装async_property,请在您的终端中运行以下命令: $ pip install async-property 或者,如果您有...
python装饰器的详细接受,适用于小白刚接触到函数,想要弄清楚什么是装饰器,希望大家喜欢,喜欢的话记得点赞哦!谢谢!!!
python装饰器的4中类型:函数装饰函数、函数装饰类、类装饰函数、类装饰类的详细说明。
python装饰器(csdn)————程序
python装饰器的引入到最终的应用实例,对python装饰的精华部分进行解读,明确写出了理解语法的关键要点。
Python 装饰器如果函数 xxx_1 作为参数传入 xxx_2 函数并被其使用,且在最后改变了 xxx_2 函数的返回结果,我们便称函数 xxx_2 为装饰