飘逸的python - property及实现lazy property
@property有什么用呢?表面看来,就是将一个方法用属性的方式来訪问.
上代码,代码最清晰了.
class Circle(object):
def __init__(self, radius):
self.radius = radius @property
def area(self):
return 3.14 * self.radius ** 2 c = Circle(4)
print c.radius
print c.area
能够看到,area尽管是定义成一个方法的形式,可是加上@property后,能够直接c.area,当成属性訪问.
如今问题来了,(不是挖掘机技术哪家强),每次调用c.area,都会计算一次,太浪费cpu了,如何才干仅仅计算一次呢?这就是lazy property.
class lazy(object):
def __init__(self, func):
self.func = func def __get__(self, instance, cls):
val = self.func(instance)
setattr(instance, self.func.__name__, val)
return val class Circle(object):
def __init__(self, radius):
self.radius = radius @lazy
def area(self):
print 'evalute'
return 3.14 * self.radius ** 2 c = Circle(4)
print c.radius
print c.area
print c.area
print c.area
能够看到,'evalute'仅仅输出了一次.假设看了我前面几篇博文,对@lazy的机制应该非常好理解.
在这里,lazy类有__get__方法,说明是个描写叙述器,第一次运行c.area的时候,由于顺序问题,先去c.__dict__中找,没找到,就去类空间找,在类Circle中,有area()方法,于是就被__get__拦截.
在__get__中,调用实例的area()方法算出结果,并动态给实例加入个同名属性把结果赋给它,即加到c.__dict__中去.
再次运行c.area的时候,先去c.__dict__找,由于此时已经有了,就不会经过area()方法和__get__了.
飘逸的python - property及实现lazy property的更多相关文章
- Python延迟初始化(lazy property)
转自:https://blog.csdn.net/azsx02/article/details/77649527 Python 对象的延迟初始化是指,当它第一次被创建时才进行初始化,或者保存第一次创建 ...
- Python descriptor 以及 内置property()函数
Python Descriptor 1, Python Descriptor是这样一个对象 它按照descriptor协议, 有这样的属性之一 def __get__(self, obj, type ...
- Python使用property函数和使用@property装饰器定义属性访问方法的异同点分析
Python使用property函数和使用@property装饰器都能定义属性的get.set及delete的访问方法,他们的相同点主要如下三点: 1.定义这些方法后,代码中对相关属性的访问实际上都会 ...
- 第8.27节 Python中__getattribute__与property的fget、@property装饰器getter关系深入解析
一. 引言 在<第7.23节 Python使用property函数定义属性简化属性访问的代码实现>和<第7.26节 Python中的@property装饰器定义属性访问方法gette ...
- Python内置函数(63)——property
英文文档: class property(fget=None, fset=None, fdel=None, doc=None) Return a property attribute. fget is ...
- Python内置函数(51)——property
英文文档: class property(fget=None, fset=None, fdel=None, doc=None) Return a property attribute. fget is ...
- <property name="current_session_context_class">thread</property> 属性
<property name="current_session_context_class">thread</property>这个属性的作用:这样配置是本 ...
- 飘逸的python - 性能调优利器profile及其意义
VIM 的作者Bram Moolenaar在一篇叫高效文本编辑器的7个习惯的ppt中有这么一段话. Three basic steps 1. Detect inefficiency 2. ...
- 飘逸的python - __new__、__init__、__call__傻傻分不清
__new__: 对象的创建,是一个静态方法.第一个參数是cls.(想想也是,不可能是self,对象还没创建,哪来的self) __init__ : 对象的初始化, 是一个实例方法,第一个參数是sel ...
随机推荐
- 解决create-react-app 后 npm start 中出现 的webpack版本问题和webpack-dev-server的版本问题
利用VSCode搭建react的脚手架运行环境的时候.create-react-app之后npm start出现如下图的问题: There might be a problem with the pr ...
- RecyclerView具体解释
public class RecyclerView extends ViewGroup implements ScrollingView, NestedScrollingChild { 由上面的继承结 ...
- poj 1001 java大精度
import java.io.* ; import java.math.* ; import java.util.* ; import java.text.* ; public class Main ...
- 8lession-基础类型转化
Python数据类型转换 有时候,我们需要对数据内置的类型进行转换,数据类型的转换,你只需要将数据类型作为函数名即可. 以下几个内置的函数可以执行数据类型之间的转换.这些函数返回一个新的对象,表示转换 ...
- ContentValues的使用
什么是 ContentValues类? ContentValues类和 Hashtable比较类似,它也是负责存储一些名值对,但是它存储的名值对当中的名是一个String类型,而值都是基本类型. 插入 ...
- gomail发送附件
采用github.com/go-gomail/gomail/ 的邮件功能,可以发送附件 以及html文档,下面是其给出的demo,测试通过. package main //cmd: go get go ...
- golang sync.Mutex
//go func 和主线程之间的关系是并行和竞争关系 package main import ( "fmt" "sync" "time" ...
- echarts如何设置背景图的颜色
公司的业务涉及到统计图的有很多,最近一直echarts里面踩各种坑,感觉应该建立一个echarts专题才对,前端的东西博大精深,无论在哪一个知识点,只要细细深究,都是别有一方天地在等待,随着需求的不同 ...
- Dialog和FormView如何派生通用类
派生通用类涉及到派生类的构造函数需要传递窗口ID和CWnd,所以要在派生类中事先定义好 在Dialog中构造函数是这样定义的 public: CDialogEx(); CDialogEx(UINT n ...
- mycat 之datanode datahost writehost readhost 区别(转)
<?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> &l ...