class Lazyproperty:
def __init__(self, func):
self.func = func def __get__(self, instance, owner):
print('get')
# print(instance)
# print(owner)
if instance is None:
return self res = self.func(instance)
setattr(instance, self.func.__name__, res)
return res class Room:
def __init__(self, width, length):
self.width = width
self.length = length @Lazyproperty
def area(self):
return self.width * self.length @property
def area1(self):
return self.width * self.length r = Room(1, 2) # print(Room.area) print(r.area1) print(r.area)
print(r.__dict__)
print(r.area)

自定制property的更多相关文章

  1. 利用描述符自定制property

    利用描述符自定制property class Lazyproperty: def __init__(self,func): # print('==========>',func) self.fu ...

  2. python - 自定制property/property的延时计算

    自定制prooerty: #模拟@property 实现将类的函数属性变成类属性: #定义描述符 class msf(): def __init__(self,obj): self.obj = obj ...

  3. 利用类装饰器自定制property实现延迟计算

    class LazyProperty: ''' hello,我是非数据描述符(没有定义__set__,不然是大哥数据描述符了--!) ''' def __init__(self, func): pri ...

  4. 11.python描述符---类的装饰器---@property

    描述符1.描述符是什么:描述符本质就是一个新式类,在这个新式类中,至少实现了__get__(),__set__(),__delete__()这三个内置方法中的一个,描述符也被称为描述符协议(1):__ ...

  5. python基础----再看property、描述符(__get__,__set__,__delete__)

    一.再看property                                                                          一个静态属性property ...

  6. day28-python之property

    1.property用法 # class Goods: # def __init__(self): # # 原价 # self.original_price = 100 # # 折扣 # self.d ...

  7. Python学习第二十课——自定property and classmethod

    自定制property class Lazyproperty: def __init__(self,func): # print('==========>',func) self.func=fu ...

  8. python基础-面向对象进阶

    一.什么是反射 反射的概念是由Smith在1982年首次提出的,主要是指程序可以访问.检测和修改它本身状态或行为的一种能力(自省).这一概念的提出很快引发了计算机科学领域关于应用反射性的研究.它首先被 ...

  9. Python之路【第六篇】python基础 之面向对象进阶

    一 isinstance(obj,cls)和issubclass(sub,super) isinstance(obj,cls)检查是否obj是否是类 cls 的对象  和  issubclass(su ...

随机推荐

  1. C语言结构体变量私有化

    操作系统 : CentOS7.3.1611_x64 gcc版本 :4.8.5 问题描述 C语言结构体定义中的变量默认是公有(Public)属性,如果实现成员变量的私有(Private)化? 解决方案 ...

  2. Spring Boot用Cxf的jax-ws开发WebService

    首先上项目的pom.xml: <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&q ...

  3. mysql alter 添加索引

    1.添加主键索引 ALTER TABLE `table_name` ADD PRIMARY KEY (`column`) 2.添加唯一索引 ALTER TABLE `table_name` ADD U ...

  4. systemctl -- 系统服务管理器 【转】

    systemctl  -- 系统服务管理器 systemctl 是系统服务管理器命令,它实际上将 service 和 chkconfig 这两个命令组合到一起. 直接运行命令可以列出所有正在运行的服务 ...

  5. matplotlib 与 seaborn 中出现中文乱码的解决方法

      Linux.Mac osx 系统中,出现 matplotlib 或 seaborn 绘图中有中文乱码的情形,可以考虑使用以下方式处理: 到 anaconda 的 matplotlib 中查看是否有 ...

  6. Visual Studio 2015 msvsmon.exe crashed when c++ debugging with x64

    Completely uninstalling Astrill fixed the issue but this solution is not what I want. Astrill suppor ...

  7. [20170713] 无法访问SQL Server

    背景: 朋友的环境第二天突然访问不了SQL Server,远程SQL Server用户无法登陆,但是本地SQL Server用户登录正常. 报错: 用户XX登录失败(MicroSoft SQL Ser ...

  8. maven私服不能重复部署解决

    1.报错 Return code is: 400, ReasonPhrase: Repository does not allow updating assets: maven-releases. 2 ...

  9. 一分钟内搭建全web的API接口神器json-server详解

    JSON-Server 是一个 Node 模块,运行 Express 服务器,你可以指定一个 json 文件作为 api 的数据源. 安装json-server npm install -g json ...

  10. tf训练OTSU

    训练一个简单的回归网络 基础的函数如下: # coding=utf-8 import tensorflow as tf import numpy as np np.random.seed(0) # 卷 ...