cached_property的使用
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的使用的更多相关文章
- 第六种方式,python使用cached_property缓存装饰器和自定义cached_class_property装饰器,动态添加类属性(三),selnium webdriver类无限实例化控制成单浏览器。
使用 from lazy_object_proxy.utils import cached_property,使用这个装饰器. 由于官方的行数比较少,所以可以直接复制出来用自己的. class cac ...
- [python]@cached_property缓存装饰器
cached_property缓存装饰器 class cached_property(object): """ Decorator that converts a met ...
- Python之路,Day8 - Python基础 面向对象高级进阶与socket基础
类的成员 类的成员可以分为三大类:字段.方法和属性 注:所有成员中,只有普通字段的内容保存对象中,即:根据此类创建了多少对象,在内存中就有多少个普通字段.而其他的成员,则都是保存在类中,即:无论对象的 ...
- Python-面向对象(类)二
一.成员修饰符 • 共有成员 • 私有成员 __+字段 __:成员修饰符 无法直接访问,只能通过该成员所属类的方法简介访问 class Foo: def __init__(self, name, ag ...
- Python 面向对象 中高级
类成员: #字段 普通字段 属于对象 执行只能通过对象访问 静态字段 属于类 执行 既可以通过对象访问,也可以通过类访问 class Foo: def __init__(self,name): # 字 ...
- python面向对象进阶(八)
上一篇<Python 面向对象初级(七)>文章介绍了面向对象基本知识: 面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使 ...
- Python自动化之django的ORM操作——Python源码
""" The main QuerySet implementation. This provides the public API for the ORM. " ...
- python 面向对象(进阶篇)
上一篇<Python 面向对象(初级篇)>文章介绍了面向对象基本知识: 面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使 ...
- Python基础篇【第7篇】: 面向对象(2)
上一篇<初识面向对象>文章介绍了面向对象基本知识: 面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使用(可以讲多函数中公 ...
随机推荐
- 软件开发者路线图梗概&书摘chapter4
准确的自我评估:不是超出平庸,而是度量能力,做到更好,谦卑 1.只求最差:学期曲线趋平 更好团队→提供帮助+准确的自我评估 列举团队并排序 2.找人指导:学习前头的人,寻找师傅 加入社区,寻找活跃的老 ...
- [工作积累] shadow map问题汇总
1.基本问题和相关 Common Techniques to Improve Shadow Depth Maps: https://msdn.microsoft.com/en-us/library/w ...
- SQL server 建立标后,执行代码添加外键
alter table dbo.student add constraint FK_tstudent_class foreign key(classno) references dbo.class(c ...
- 使用Postfix与Dovecot部署邮件系统
- 使用IDEA创建SSM框架
- 打印杨辉三角—Python
b=[] for i in range(0,9): c=[] for j in range(0,i): if j==0: c.append(b[i-1][j]) if j<=i-2:#执行完第一 ...
- frost_vex_01
int inc = 0; //整数inc等于0 while(inc < 6){ //inc在小于6的范围内递增 if(rand(@ptnum + inc + ch("seed" ...
- 使用Visual Studio 2017开发Linux程序
环境: win7_x64旗舰版.VS2017企业版.VMware10.0.2.CentOS7 在CentOS7上首先需要安装gcc.g++和gdbserver,这里就不多说 一.安装VS2017 1. ...
- 使用Python matplotlib做动态曲线
今天看到“Python实时监控CPU使用率”的教程: https://www.w3cschool.cn/python3/python3-ja3d2z2g.html 自己也学习如何使用Python ma ...
- bat文件与Vbs文件常用操作(获取用户输入,执行VBS文件)
bat文件: set /P StrInput="输入数字:" echo 输入的数字为%StrInput% set /P Flg="是否执行(y/n):" IF ...