python 旧类中使用property特性的方法
在python中,我们可以拦截对象的所有特性访问。通过这种拦截的思路,我们可以在旧式类中实现property方法。
__getattribute__(self, name) #当特性name被访问时自动调用(只能在新式类中使用)
__getattr__(self, name) #当特性name被访问且对象没有相应的特性时被自动调用
__setattr__(self, name, value) #当试图给特性name赋值时会被自动调用
__delattr__(self, name) #当试图删除特性name时被自动调用 #*相比于使用property有点复杂,但是特殊方法用途很广
下面是举例:
class Demo5(object):
def __init__(self):
print("这是构造函数")
self._value1 = None
self._value2 = None def __setattr__(self, key, value):
print("try to set the value of the %s property" % (key,))
if key == 'newValue':
self._value1, self._value2 = value
else:
print("the property key is not newValue, now create or set it through __dict__")
self.__dict__[key] = value def __getattr__(self, item):
print("try to get the value of %s " % (item,))
if item == 'newValue':
return self._value1, self._value2
else:
print("the %s item is not exist" % (item,))
#raise AttributeError def __delattr__(self, item):
if item == 'newValue':
print("delete the value of newValue")
del self._value1
del self._value2
else:
print("delete other values ,the value name is %s" % item) if __name__ == "__main__":
testDemo = Demo5()
print(testDemo.newValue)
testDemo.newValue = "name","helloworld"
print("print the value of property:", testDemo.newValue)
print(testDemo.notExist)
del testDemo.newValue
下面是结果:
这是构造函数
try to set the value of the _value1 property
the property key is not newValue, now create or set it through __dict__
try to set the value of the _value2 property
the property key is not newValue, now create or set it through __dict__
try to get the value of newValue
(None, None)
try to set the value of the newValue property
try to set the value of the _value1 property
the property key is not newValue, now create or set it through __dict__
try to set the value of the _value2 property
the property key is not newValue, now create or set it through __dict__
try to get the value of newValue
('print the value of property:', ('name', 'helloworld'))
try to get the value of notExist
the notExist item is not exist
None
delete the value of newValue
delete other values ,the value name is _value1
delete other values ,the value name is _value2
**其实,我们可以发现在使用__setatter__ , __getatter__,__delatter__这些接口时会对其他的值造成影响,因为这个是通用的必须调用的接口。
python 旧类中使用property特性的方法的更多相关文章
- python定义类()中写object和不写的区别
这里需要说明一下: python3中,类定义默认继承object,所以写不写没有区别 但在python2中,并不是这样 所以此内容是针对python2的,当然python3默认继承,不代表我们就傻乎乎 ...
- 【python】类中的self
在python的类中,经常会写self,代表对象自己.如下例: #coding=utf-8 class Foo: def __init__(self, name): self.name = name ...
- Python: 类中为什么要定义__init__()方法
学习并转自:https://blog.csdn.net/geerniya/article/details/77487941 1. 不用init()方法定义类 定义一个矩形的类,目的是求周长和面积. c ...
- 实现Square类,让其继承自Rectangle类,并在Square类增添新属性和方法,在2的基础上,在Square类中重写Rectangle类中的初始化和打印方法
实现Square类,让其继承自Rectangle类,并在Square类增添新属性和方法,在2的基础上,在Square类中重写Rectangle类中的初始化和打印方法 #import <Found ...
- 类中为什么要定义__init__()方法
总结一下, 加上__init__()方法后,类才可以实例化,不加类就是个空壳,相当于一个方法集合 学习Python的类,一直不太理解为什么一定要定义init()方法,现在简要谈一下自己的理解吧. 1. ...
- C#判断一个类中有无"指定名称"的方法
C#中可以通过反射分析元数据来解决这个问题,示例代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 2 ...
- QRadioButton类中Toggled()信号的使用方法
QRadioButton类中Toggled()信号的使用方法 1.说明 QRadioButton中,Toggled()信号是在Radio Button状态(开.关)切换时发出的,而clicked()信 ...
- File类中的list和listFiles方法
File类中的list和listFiles方法 list()方法是返回某个目录下的所有文件和目录的文件名,返回的是String数组 listFiles()方法是返回某个目录下所有文件和目录的绝对路径, ...
- Python+selenium自动化测试中Windows窗口跳转方法
Python+selenium自动化测试中Windows窗口跳转方法 #第一种方法 #获得当前窗口 nowhandle=driver.current_window_handle #打开弹窗 drive ...
随机推荐
- 【记忆化搜索】bzoj3208 花神的秒题计划Ⅰ
暴力 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #de ...
- [TC-HouseProtection]House Protection
题目大意: 一个平面直角坐标系中有给定的$n(n\le50)$个红点和$m(m\le50)$个蓝点,每个点可以选择画一个半径为$r$(所有的$r$相同)的圆或不画.圆的半径上限为$R(R\le1000 ...
- RxJava 2.x 理解-1
在RxJava 1.x 系列中,讲解了RxJava的大致用法,因为现在都用RxJava 2了,所以Rxjava 1就不细讲,主要来学习RxJava 2. 基本使用: /** * rajava2 的基本 ...
- 【转载】linux2.6内核initrd机制解析
题记 很久之前就分析过这部分内容,但是那个时候不够深入,姑且知道这么个东西存在,到底怎么用,来龙去脉咋回事就不知道了.前段时间工作上遇到了一个initrd的问题,没办法只能再去研究研究,还好,有点眉目 ...
- IntelliJ IDEA强制更新Maven的包
1.手动删除Project Settings里面的Libraries内容,[Ctrl]+[Alt]+[Shift]+[S],全选之后点击左上角的减号按钮. 2.在Maven Project的试图里的L ...
- linux+iptables搭建网关服务器
公司购买的一批云服务器只带内网,配置了一个负载均衡器(lb),这批服务器通过lb可以对外提供服务,但是这批服务器不能主动连接外网,例如使用wget下载文件,或者curl访问ttlsa.com站点. 额 ...
- SpringBoot下文件上传与下载的实现
原文:http://blog.csdn.net/colton_null/article/details/76696674 SpringBoot后台如何实现文件上传下载? 最近做的一个项目涉及到文件上传 ...
- mysql left join,right join,inner join的区别
left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录 inner join(等值连接) ...
- Jquery获取对象的几种方式介绍
参考: 1.http://blog.csdn.net/zengyonglan/article/details/53995295 2.http://api.jquery.com/category/sel ...
- 【Cocos2d-x 3.0 基础系列一】 各类回调函数写法汇总
一.button回调 1. Lambda 表达式,C++11 Lambda 赋予了Cocos2d-x 3.0创建回调函数的灵活性. auto itemNor = Sprite::create(&quo ...