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)
上一篇<初识面向对象>文章介绍了面向对象基本知识: 面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使用(可以讲多函数中公 ...
随机推荐
- C++ 基于凸包的Delaunay三角网生成算法
Delaunay三角网,写了用半天,调试BUG用了2天……醉了. 基本思路比较简单,但效率并不是很快. 1. 先生成一个凸包: 2. 只考虑凸包上的点,将凸包环切,生成一个三角网,暂时不考虑Delau ...
- 左耳听风-ARTS-第3周(2019/4/7-2019/4/13)
Algorithm 本周的算法题是按顺序合并两个已排序的链表(https://leetcode.com/problems/merge-two-sorted-lists/).和归并排序的合并已排序数组的 ...
- git代理配置
命令行模式下配置 git config --global https.proxy https://proxyuser:proxypassword@ip/域名:port git config --glo ...
- 通过ZipKin整理调用链路
缘由 公司使用的是Docker+微服务,服务拆分差不多41个了,然后过完年来就接到这个需求,把指定业务功能的业务基线整理出来,比如,登录这个操作会经过哪些微服务,把登录这个操作的链条列出来,从api- ...
- day02 格式化字符串
字符格式化2019-04-01 方法一 通过f + {} 格式化字符串 name = input("Name: ")age = input("Age:")sco ...
- orchestrator HTTP接口forget-cluster误下线集群问题
orchestrator 提供了"forget-cluster"HTTP接口用于下线集群.该接口可以根据提供的参数,推测可能的集群名cluster name,然后使用cluster ...
- HTTP协议初步解析
一.什么是HTTP协议 HTTP是hypertext transfer protocol(超文本传输协议)的简写,它是TCP/IP协议的一个应用层协议,定义了Web客户端向Web服务器请求Web页面的 ...
- 【Leecode】两数相加
学习了链表结构,链表中元素的访问,对象指针的初始化与赋值,链表的创建(多个节点链接起来),进位计算的表达. 100ms /** * Definition for singly-linked list. ...
- 内存问题-JVM调优思路
通常,内存的问题就是 GC 的问题,因为 Java 的内存由 GC 管理.有2种情况,一种是内存溢出了,一种是内存没有溢出,但 GC 不健康. 内存溢出的情况可以通过加上 -XX:+HeapDumpO ...
- mplayer用法收集【转】
转自:https://blog.csdn.net/wylhistory/article/details/4816653 1,录音: mplayer mms://202.***.***.***/test ...