@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的更多相关文章

  1. Python延迟初始化(lazy property)

    转自:https://blog.csdn.net/azsx02/article/details/77649527 Python 对象的延迟初始化是指,当它第一次被创建时才进行初始化,或者保存第一次创建 ...

  2. Python descriptor 以及 内置property()函数

    Python Descriptor  1, Python Descriptor是这样一个对象 它按照descriptor协议, 有这样的属性之一 def __get__(self, obj, type ...

  3. Python使用property函数和使用@property装饰器定义属性访问方法的异同点分析

    Python使用property函数和使用@property装饰器都能定义属性的get.set及delete的访问方法,他们的相同点主要如下三点: 1.定义这些方法后,代码中对相关属性的访问实际上都会 ...

  4. 第8.27节 Python中__getattribute__与property的fget、@property装饰器getter关系深入解析

    一. 引言 在<第7.23节 Python使用property函数定义属性简化属性访问的代码实现>和<第7.26节 Python中的@property装饰器定义属性访问方法gette ...

  5. Python内置函数(63)——property

    英文文档: class property(fget=None, fset=None, fdel=None, doc=None) Return a property attribute. fget is ...

  6. Python内置函数(51)——property

    英文文档: class property(fget=None, fset=None, fdel=None, doc=None) Return a property attribute. fget is ...

  7. <property name="current_session_context_class">thread</property> 属性

    <property name="current_session_context_class">thread</property>这个属性的作用:这样配置是本 ...

  8. 飘逸的python - 性能调优利器profile及其意义

    VIM 的作者Bram Moolenaar在一篇叫高效文本编辑器的7个习惯的ppt中有这么一段话. Three basic steps 1.    Detect inefficiency 2.    ...

  9. 飘逸的python - __new__、__init__、__call__傻傻分不清

    __new__: 对象的创建,是一个静态方法.第一个參数是cls.(想想也是,不可能是self,对象还没创建,哪来的self) __init__ : 对象的初始化, 是一个实例方法,第一个參数是sel ...

随机推荐

  1. hdu 3294 Girls&#39; research

    #include<stdio.h> #include<string.h> #define MAX 200020 char s[MAX],ss[MAX*2],str[2]; in ...

  2. webservie授权调用

    Dim usercode As String = System.Configuration.ConfigurationSettings.AppSettings("SAPWebServiceU ...

  3. Java学习笔记三.3

    9.异常处理:Java中的异常处理对象就是将以前的if语句进行的判断进行抽象化,并形成的一套错误处理体系.最顶端是Throwable,接着是Error,Exception,其中Exception又明显 ...

  4. golang sync.RWMutex

    sync.RWMutex package main import ( "fmt" "runtime" "sync" ) func click ...

  5. Vue的学习--遇到的一些问题和解决方法

    包括: 1.Missing space before function parentheses 2.如何给.vue文件的页面添加css 3.如何给.vue文件页面里的元素添加监听器 4.如何为每一个页 ...

  6. sass自定义滚动条样式

    @mixin scrollBarStyle() { &::-webkit-scrollbar { width: 7px; height: 7px; } &::-webkit-scrol ...

  7. [D3] Draw a basic US d3-geo map

    Install: npm install --save d3 d3-geo topojson Code: import React, {Component} from 'react'; import ...

  8. 二叉树的递归插入【Java实现】

    C++中由于有指针的存在,可以让二叉树节点指针的指针作为插入函数的实参,在函数体内通过*操作实现对真实节点指针.节点左孩子指针.节点右孩子指针的改变,这样很容易使用递归将大树问题转化到小树问题.但在J ...

  9. vue实现一个会员卡的组件(可以动态传入图片(分出的一个组件)、背景、文字、卡号等)

    自己在写这个组件的时候主要遇到的问题就是在动态传入背景图片或者背景色的时候没能立马顺利写出来,不过现在实现了这个简单组件就和大家分享一下 <template> <div class= ...

  10. Leetcode-求两数之和

    题目: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中 ...