python 旧类中使用property特性的方法
2018-05-22 16:10
330 查看
在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基础----特性(property)、静态方法(staticmethod)、类方法(classmethod)、__str__的用法
- python 3全栈开发-面向对象之绑定方法(classmethod与staticmethod的区别)、多态、封装的特性property
- python property方法的使用
- 升级Python导致Yum服务不能使用的修复方法
- 【用Python写爬虫】获取html的方法【一】:使用urllib
- 【用Python写爬虫】获取html的方法【二】:使用pycurl
- 【用Python写爬虫】获取html的方法【一】:使用urllib
- 【用Python写爬虫】获取html的方法【三】:使用cPAMIE
- 任何人都可以重构---使用 Eclipse 自动重构特性的方法与原因 选择自 shanyou 的 Blog
- Python strip lstrip rstrip使用方法
- python下使用qrcode方法
- C#嵌套类的使用方法及特性(转)
- p4.pm p4python p4perl p4api 的使用方法
- Python __getattr__与__setattr__使用方法
- 升级Python而导致的YUM服务无法使用的修复方法
- C# 3.0新特性初步研究 Part2:使用扩展方法_C#教程
- C# 3.0新特性初步研究 Part2:使用扩展方法_C#教程
- 改善C#程序的50种方法 条款4:使用Conditional特性代替#if条件编译
- Python httplib,smtplib使用方法