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. ionic actionsheet在android下的样式问题

    https://forum.ionicframework.com/t/actionsheets-android-ugly-styling-need-help/18462/10 想要修改ionic的样式 ...

  2. 在Centos7下安装nghttp2

    如果是Ubuntu18.04, 系统本身已经带了nghttp2了, 直接apt安装就可以. 下载源代码 https://github.com/nghttp2/nghttp2 如果是在Ubuntu下编译 ...

  3. SpUtil多样加密存储,兼容android9.0

    代码地址如下:http://www.demodashi.com/demo/15058.html 前言 在android系统不断升级的过程中,Sharepreferences存储出现多中问题,其中有些是 ...

  4. Couldn't find log associated with operation handle: OperationHandle [opType=EXECUTE_STATEMENT, getHandleIdentifier ()=5687ff62-aa71-4b47-af6c-89f6a3f7a1fe]

    这个异常的出现是因为hive-site-xml中的hive.server2.logging.operation.log.location属性未配置正确: 修改为: <property> & ...

  5. Mac下的Chrome或Safari访问跨域设置,MBP上使用模拟器Simulator.app或iphone+Safari调试网页

    Mac下的Chrome或Safari访问跨域设置: mac下终端启动Chrome $ open -a Google\ Chrome --args --disable-web-security 或 /A ...

  6. 【C++】C++中的字符和字符串

    目录结构: contents structure [-] 定义和初始化string string对象上的操作 处理string对象中的字符 C风格字符串 标准库类型string表示可变长的字符序列,使 ...

  7. bootstrap-datepicker应用

    参考本人的github:https://github.com/gmqllf/Datepicker-for-Bootstrap (fork官方的) 一.使用datepicker for bootstra ...

  8. Fedora 25-64位操作系统中安装配置Hyperledger Fabric过程

    安装过程参照Hyperledger Fabric的官方文档,文档地址:http://hyperledger-fabric.readthedocs.io/en/latest/prereqs.html 0 ...

  9. why-the-default-authentication-hadoop-is-unsecured ?

    https://www.learningjournal.guru/article/hadoop/hadoop-security-using-kerberos/ https://stackoverflo ...

  10. php -- 断点调试 之 选择合适的xdebug

    这里不讲如何在不同的ide里安装断点调试,讲一个不起眼却很容易犯的错误: 如何寻找适合你的环境的xdebug! 不要小看这个问题,如果说xdebug都错了,你再怎么安装断点调试,都不会成功,反而还找不 ...