cached_property修饰过的函数,变成是对象的属性,该对象第一次引用该属性时,会调用函数,对象第二次引用该属性时就直接从词典中取了,这也说明引用属性是经过__getattritue__。

class cached_property(object):
"""
Decorator that converts a method with a single self argument into a
property cached on the instance. Optional ``name`` argument allows you to make cached properties of other
methods. (e.g. url = cached_property(get_absolute_url, name='url') )
"""
def __init__(self, func, name=None):
self.func = func
self.__doc__ = getattr(func, '__doc__')
self.name = name or func.__name__ def __get__(self, instance, type=None):
if instance is None:
return self
res = instance.__dict__[self.name] = self.func(instance)
return res
class Monopoly(object): def __init__(self):
self.boardwalk_price = 500 @cached_property
def boardwalk(self):
# Again, this is a silly example. Don't worry about it, this is
# just an example for clarity.
self.boardwalk_price += 50
return self.boardwalk_price monopoly = Monopoly()
print monopoly.boardwalk
print monopoly.boardwalk
print monopoly.boardwalk
del monopoly.__dict__['boardwalk']
print monopoly.boardwalk
print monopoly.boardwalk

输出为:

*** Remote Interpreter Reinitialized  ***
>>>
550
550
550
600
600
>>>

cached_property的使用的更多相关文章

  1. 第六种方式,python使用cached_property缓存装饰器和自定义cached_class_property装饰器,动态添加类属性(三),selnium webdriver类无限实例化控制成单浏览器。

    使用 from lazy_object_proxy.utils import cached_property,使用这个装饰器. 由于官方的行数比较少,所以可以直接复制出来用自己的. class cac ...

  2. [python]@cached_property缓存装饰器

    cached_property缓存装饰器 class cached_property(object): """ Decorator that converts a met ...

  3. Python之路,Day8 - Python基础 面向对象高级进阶与socket基础

    类的成员 类的成员可以分为三大类:字段.方法和属性 注:所有成员中,只有普通字段的内容保存对象中,即:根据此类创建了多少对象,在内存中就有多少个普通字段.而其他的成员,则都是保存在类中,即:无论对象的 ...

  4. Python-面向对象(类)二

    一.成员修饰符 • 共有成员 • 私有成员 __+字段 __:成员修饰符 无法直接访问,只能通过该成员所属类的方法简介访问 class Foo: def __init__(self, name, ag ...

  5. Python 面向对象 中高级

    类成员: #字段 普通字段 属于对象 执行只能通过对象访问 静态字段 属于类 执行 既可以通过对象访问,也可以通过类访问 class Foo: def __init__(self,name): # 字 ...

  6. python面向对象进阶(八)

    上一篇<Python 面向对象初级(七)>文章介绍了面向对象基本知识: 面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使 ...

  7. Python自动化之django的ORM操作——Python源码

    """ The main QuerySet implementation. This provides the public API for the ORM. " ...

  8. python 面向对象(进阶篇)

    上一篇<Python 面向对象(初级篇)>文章介绍了面向对象基本知识: 面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使 ...

  9. Python基础篇【第7篇】: 面向对象(2)

    上一篇<初识面向对象>文章介绍了面向对象基本知识: 面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使用(可以讲多函数中公 ...

随机推荐

  1. 私有IP

    私有IP地址段为10.0.0.0-10.255.255.255,172.16.0.0-172.31.255.255,192.168.0.0-192.168.255.255.

  2. C++字节对齐与位域

    环境: win7_x64旗舰版.VS2015企业版 一.字节对齐: 说明:为了提高 CPU 的存储速度,编译器会对 struct 和 union的存储进行优化,即进行字节对齐. 1. 指定对齐参数值: ...

  3. Docker系列(一)CentOS 6.5 离线安装、不升级内核

    本特安装教程特点 1.由于是离线下载,需要提前下载好需要的依赖包 2.使用的版本为Centos6.5 3.不升级内核 4.提供异常解决方案. 安装过程 一.下载依赖包(使用能联网的节点) 依赖包可以自 ...

  4. java-项目中无法访问js、css等静态资源

    解决方法:在mvc.xml配置文件中增加如下配置 如果增加<mvc:default-servlet-handler/> 后无法访问controller,需要增加<mvc:annota ...

  5. cut字符串截取

    cut字符串截取 -d 按字节截取 [root@slave elk]# ll total 0 drwxr-xr-x. 6 root root 194 Jan 24 16:15 bigdesk 截取前2 ...

  6. 视频外同步信号研究---fvh

    视频外同步信号研究---fvh 一个时钟周期有两个edge,分别称为:(1)Leading edge=前一个边沿=第一个边沿,对于开始电压是1,那么就是1变成0的时候:对于开始电压是0,那么就是0变成 ...

  7. debian shell脚本关联

    懒得命令行一个个的输 设置,MIME类型编辑,搜索x-shellscript,默认的改成/bin/bash即可

  8. 黄聪:PHP获取某一天前后任意时间

    date("Y-m-d",strtotime('-30 days',strtotime('2016-9-30')));

  9. 【CentOS】自定义服务添加

    1.创建服务需要执行的脚本 cd /sb/scripts vi td-agent-testlog #!/bin/bash # # /etc/rc.d/init.d/td-agent-DC01 # # ...

  10. windows 8.1 启用hyper-v导致vmware 无法使用的问题解决方案(兼顾WP8.1模拟器和vmware)

    最近搭建了windows phone 8.1开发环境,为了开机就可以进行WP8.1开发,就使用了 bcdedit /set {BCD ID} hypervisorlaunchtype auto 命令将 ...