自定制property
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的更多相关文章
- 利用描述符自定制property
利用描述符自定制property class Lazyproperty: def __init__(self,func): # print('==========>',func) self.fu ...
- python - 自定制property/property的延时计算
自定制prooerty: #模拟@property 实现将类的函数属性变成类属性: #定义描述符 class msf(): def __init__(self,obj): self.obj = obj ...
- 利用类装饰器自定制property实现延迟计算
class LazyProperty: ''' hello,我是非数据描述符(没有定义__set__,不然是大哥数据描述符了--!) ''' def __init__(self, func): pri ...
- 11.python描述符---类的装饰器---@property
描述符1.描述符是什么:描述符本质就是一个新式类,在这个新式类中,至少实现了__get__(),__set__(),__delete__()这三个内置方法中的一个,描述符也被称为描述符协议(1):__ ...
- python基础----再看property、描述符(__get__,__set__,__delete__)
一.再看property 一个静态属性property ...
- day28-python之property
1.property用法 # class Goods: # def __init__(self): # # 原价 # self.original_price = 100 # # 折扣 # self.d ...
- Python学习第二十课——自定property and classmethod
自定制property class Lazyproperty: def __init__(self,func): # print('==========>',func) self.func=fu ...
- python基础-面向对象进阶
一.什么是反射 反射的概念是由Smith在1982年首次提出的,主要是指程序可以访问.检测和修改它本身状态或行为的一种能力(自省).这一概念的提出很快引发了计算机科学领域关于应用反射性的研究.它首先被 ...
- Python之路【第六篇】python基础 之面向对象进阶
一 isinstance(obj,cls)和issubclass(sub,super) isinstance(obj,cls)检查是否obj是否是类 cls 的对象 和 issubclass(su ...
随机推荐
- 使用日志服务进行Kubernetes日志采集
阿里云容器服务Kubernetes集群集成了日志服务(SLS),您可在创建集群时启用日志服务,快速采集Kubernetes 集群的容器日志,包括容器的标准输出以及容器内的文本文件. 新建 Kubern ...
- 【Tensorflow】Tensorflow r1.0, Ubuntu, gpu, conda安装说明
Install Anaconda and python 1. cuda-8.0 download cuda_8.0.61_375.26_linux.run ./cuda_8.0.61_375.26_l ...
- Window 包管理工具: chocolatey
传送门 # 官网 https://chocolatey.org/install # 发生错误看看这个https://yevon-cn.github.io/2017/03/12/install-choc ...
- Linux系统中的常用命令
查看日志 cat 或 tail -f 日志文件说明 /var/log/message 系统启动后的信息和错误日志,是Red Hat Linux中最常用的日志之一 /var/log/secure 与安全 ...
- 用virsh console vhosts 卡住
[root@666 ok]# virsh list --all Id Name State ---------------------------------------------------- 1 ...
- select 与 time.After 配合使用的问题
今天在工作中发现了一个有趣的现象. 在一个select中设定了两个定时器,本来预计哪个定时器到达就运行相应指令的,但是发现最终只有时间最短的定时器一直得到执行,其它定时器完全没有得到执行. packa ...
- elastic search query & filter & query_string
一.基本概念 1.query时,如何指定返回哪些字段 希望返回name和date字段 希望返回以location.*为前缀的字段以及date字段:不希望返回location.geolocation字段 ...
- 【转载】MapReduce编程 Intellij Idea配置MapReduce编程环境
目录(?)[-] 一软件环境 二创建maven工程 三添加maven依赖 四配置log4j 五启动Hadoop 六运行WordCount从本地读取文件 七运行WordCount从HDFS读取文件 八代 ...
- Oracle中判断字段是否为数字
在我们平常的开发中可能会遇到这样的问题,就是判断某一列是否全部由数字组成,我们都知道oracle并没有给我们提供这样一个现成的函数,那么根据我的经验我总结了两个行之有效的方法(列名:column,表名 ...
- C语言 · 猜算式 · 乘法竖式
题目:猜算式 你一定还记得小学学习过的乘法计算过程,比如: 273 x 15 ------ 1365 273 ------ 4095 请你观察如下的乘法算式 *** x *** ------- ...