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. 钱管够,你能接这个项目吗?+ tomcat源码分析

    最近看了几个咕泡学院的公开课,课堂老师讲到下面这两个经历. 1:钱给够,你有没有能力接下这个全国性的项目 平时也会有怀才不遇的时候,但是当你遇到这个机会的时候,有没有信心去接下这个单子呢? 信心和能力 ...

  2. jvm各区域OutOfMemory测试方法

    1.堆溢出 VM options:-Xmx9999k -Xmn3333k public class HeapOverMemory { public static void main(String[] ...

  3. HI3518EV200+AR0130开发板烧录uboot、kernel、rootfs及其参数配置

    分区名 分区大小 起始地址 截至地址bootloader:1M 0x00000000 0x00100000kernel: 3M 0x00100000 0x00400000rootfs: 12M 0x0 ...

  4. DNS实战--1

    DNS(Domain Name System,域名系统)因特网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户方便的访问互联网,而不用去记住能够被机器读取的IP数串.通过主机名,最终得到该主 ...

  5. a标签通过浏览器下载远程图片

    <a href="http://fooku.oss-cn-hongkong.aliyuncs.com/image/store/2nblHVyB6cWyBI7Aq2SEp6aZRBlui ...

  6. element-ui 点击获取table的行索引

    <el-table :data="list" v-loading.body="listLoading" element-loading-text=&quo ...

  7. java Servlet生成随机验证码

    import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; i ...

  8. Cause: dx.jar is missing

    Cause: dx.jar is missing 解决方案 方案一 copy dx.jar到目标编译版本 查找相应的buildToolsVersion版本下是否有dx.jar存在 如果不存在则可以co ...

  9. mac下常用命令

    常用命令 ls 查看当前目录下的文件 cd 进入某目录 . cd - 跳转回前一目录 . cd ~ 进入当前用户个人目录 pwd 输出当前所在路径 mkdir 新建文件夹. touch 新建文件 fi ...

  10. Elasticsearch-6.7.0系列(四)Metricbeat仪表盘。本身无端口,依赖kibana

    前提 centos7环境 https://www.cnblogs.com/zhuwenjoyce/p/10629320.html         elasticsearch搜索引擎 https://w ...