利用描述符自定义property
class Lazyproperty:
def __init__(self,func): #传的func函数是被描述的类中的函数属性
self.func = func
def __get__(self, instance, owner): #instance 是实例
if instance is None:
return self #当没有实例时返回自身Lazyproperty()
res = self.func(instance) #func是被描述的函数,就相当于运行area这个函数
setattr(instance,self.func.__name__,res) #将结果添加到实例字典中,但要注意,当是数据描述符时,还是从数据描述符中查找
return res class Room:
def __init__(self,name,width,length):
self.name =name
self.width = width
self.length = length @Lazyproperty #area = Lazyproperty(area) 调用描述符
def area(self):
return self.width * self.length r1 = Room("客厅",20,30)
print(Room.__dict__)
print(r1.area)
print(Room.area) #类调用静态属性时,instance实例对象是不存在的,返回时None
#property:实质实现的get,set,del
class Foo:
@property
def test(self):
print("get时运行") @test.setter #只有定义了property这个静态属性,才能设置setter
def test(self, val):
print("set时运行", val) @test.deleter
def test(self):
print("deleter时运行") def get_aaa(self):
print("get时运行")
def set_aaa(self,val):
print("set时运行",val)
def del_aaa(self):
print("del时运行") aaa = property(get_aaa,set_aaa,del_aaa) #这种等同上面的属性操作 f1 =Foo()
f1.test
f1.test="tt"
f1.aaa = "ss"
利用描述符自定义property的更多相关文章
- 利用描述符自定制property
利用描述符自定制property class Lazyproperty: def __init__(self,func): # print('==========>',func) self.fu ...
- 课时46:魔法方法:描述符(property的原理)
目录: 一.描述符(property的原理) 二.课时46课后习题及答案 ********************************** 一.描述符(property的原理) ********* ...
- Python描述符以及Property方法的实现原理
Python描述符以及Property方法的实现原理 描述符的定义: 描述符是什么:描述符本质就是一个新式类,在这个新式类中,至少实了__get__(),__set__(),__delete__()中 ...
- Python描述符:property()函数的小秘密
描述符:将某种特殊类型的类的实例指派给另一个类的属性(注意:这里是类属性,而不是对象属性).而这种特殊类型的类就是实现了__get__,__set__,__delete__这三个方法中的一个或多个的新 ...
- 描述符和property内建函数
首先我们搞清楚__getattr__ ,__get__ 和 __getattribute__ 作用的不同点. __getattr__在授权中会用到. __getattribute__ 当要访问属性时 ...
- 使用描述符实现property功能
# Author : Kelvin # Date : 2019/1/25 14:46 class Decproperty: def __init__(self, func): self.func = ...
- Py-上下文管理方法,描述符的应用,错误与异常
上下文管理方法: 可以在exit里面弄一些内存清理的功能 class Open: def __init__(self,name): self.name=name def __enter__(self) ...
- python描述符(descriptor)、属性(property)、函数(类)装饰器(decorator )原理实例详解
1.前言 Python的描述符是接触到Python核心编程中一个比较难以理解的内容,自己在学习的过程中也遇到过很多的疑惑,通过google和阅读源码,现将自己的理解和心得记录下来,也为正在为了该问题 ...
- 11.python描述符---类的装饰器---@property
描述符1.描述符是什么:描述符本质就是一个新式类,在这个新式类中,至少实现了__get__(),__set__(),__delete__()这三个内置方法中的一个,描述符也被称为描述符协议(1):__ ...
随机推荐
- Comparable接口与Comparator接口的比较————Comparator接口详解
Comparator接口位于:java.util包中. Comparator接口:1. 强行对某个对象的Collection进行整体排序.值得注意的是:Comparator接口可以作为参数传到一些so ...
- code first System.Data.Entity.Infrastructure.CommitFailedException: An error was reported while committing a database transaction but it could not be determined whether the transaction succeeded
System.Data.Entity.Infrastructure.CommitFailedException: An error was reported while committing a da ...
- kafka操作命令
kafka启动 bin/kafka-server-start.sh -daemon config/server.properties 创建topic bin/kafka-topics.sh -zook ...
- leetcode 125 验证回文字符串 Valid Palindrome
验证回文字符串 C++ 思路就是先重新定义一个string ,先遍历第一遍,字符串统一小写,去除空格:然后遍历第二遍,首尾一一对应比较:时间复杂度O(n+n/2),空间O(n); class Solu ...
- Spring 缓存注解 SpEL 表达式解析
缓存注解上 key.condition.unless 等 SpEL 表达式的解析 SpEl 支持的计算变量: 1)#ai.#pi.#命名参数[i 表示参数下标,从 0 开始] 2)#result:Ca ...
- 浅谈移动端设备标识码:DeviceID、IMEI、IDFA、UDID和UUID -费元星
在公司做数据分析的时候,发现NA端有很多ID,所有来系统的理解一下,有问题大家多指出 [心路历程] 最近刚好在思考工作中统计数据所用的标识码产生的数据误差到底有多大,借此机会几番搜索资料+请教大神 ...
- Openstack 实现技术分解 (3) 开发工具 — VIM & dotfiles
目录 目录 前文列表 扩展阅读 前言 插件管理 Vundle 主题 Solarized 浏览项目目录结构 Nerdtree Symbol 窗口 Tagbar 文件模糊查询 CtrlP 代码补全 You ...
- Visual Studio Code 断点调试Nodejs程序跳过node内部模块(internal modules)
Built-in core modules of Node.js can be referred to by the ‘magic name’ <node_internals> in a ...
- Intellij IDEA 常见问题
右击项目时,没有 Java Class,只能创建其他文件 IDEA 还没有将这个项目识别为 Maven 项目时,会出现这种情况.此时右键无法创建类. 解决办法: 手动为 IDEA 指定项目类型:如果编 ...
- Grass Planting
大致题意: 维护一棵树,支持两种操作: P x y x到y路径上的每条边的值+1:Q x y 询问x到y路径上所有边的值的和.Input第一行两个正整数,N,M表示点数和操作数:接下来N-1行每行两个 ...