Python初学之类的狭义属性(@property)
来源:互联网 发布:windows模拟器安卓版 编辑:程序博客网 时间:2024/06/03 21:15
python2和Python3的属性的处理和实现不一样,内部实现有较大差异,本文属性指的是狭义的概念。
一.Python2中的属性
在Python2中属性使用@property或者property来声明,但是属性并没有真正意义上实现过,也没有提供应有的保护机制。为对象增加新成员时,python2中原来的私有变量没有改变,只是动态的增加了新成员,隐藏了定义的属性。
>>> class A:def __init__(self,v): self.__v = v@propertydef v(self):return self.__v>>> a = A(3)>>> a.v3>>> a.v = 5#动态的添加了新成员,隐藏了定义的属性>>> a.v5>>> a._A__v#原来的私有变量没改变3下面的代码实际上也是增加了新成员,隐藏了已有的属性
>>> class A:def __init__(self,v): self.__v = vdef __get(self):return self.__vdef __set(self,v):self.__v = vv = property(__get,__set)def show(self):print self.__vdef show(self):print self.__v>>> t = A(3)>>> t.v3>>> t.v +=2#动态添加新成员>>> t.v#访问新成员5>>> t.show()#访问私有成员3>>> del t.v#删除刚添加的新成员>>> t.v#访问原来的属性3>>> del t.v#试图删除属性Traceback (most recent call last): File "<pyshell#200>", line 1, in <module> del t.vAttributeError: A instance has no attribute 'v'>>> t.v3>>> del t._A__v#删除私有成员>>> t.v#访问属性,但是私有成员已经不存在了Traceback (most recent call last): File "<pyshell#203>", line 1, in <module> t.v File "<pyshell#192>", line 6, in __get return self.__vAttributeError: A instance has no attribute '_A__v'
下面演示私有成员与普通成员之间的关系:
class A:def show(self):print self.aprint self.__b>>> a = A()>>> a.show()Traceback (most recent call last): File "<pyshell#210>", line 1, in <module> a.show() File "<pyshell#208>", line 3, in show print self.aAttributeError: A instance has no attribute 'a'>>> a.a =3>>> a.show()3Traceback (most recent call last): File "<pyshell#212>", line 1, in <module> a.show() File "<pyshell#208>", line 4, in show print self.__bAttributeError: A instance has no attribute '_A__b'>>> a.__b = 4>>> a.show()3Traceback (most recent call last): File "<pyshell#214>", line 1, in <module> a.show() File "<pyshell#208>", line 4, in show print self.__bAttributeError: A instance has no attribute '_A__b'>>> a._A__b = 5>>> a.show()35
二.python3中的属性
在python3中属性得到了较为完整的实现,支持更加全面的保护机制。如果设置属性为只读,则无法修改其值,也无法为对象增加与属性同名的新成员,同时也无法删除对象的属性。
(1)只读,不可修改和删除
class A:def __init__(self,v):self.__v = v@propertydef v(self):return self.__v>>> a = A(3)>>> a.v3>>> a.v = 4#只读属性不许修改
>>>AttributeError: cannt set attribute>>> a.vv = 5#动态增加形成员>>> a.vv5>>> del a.vv#动态删除成员>>> del a.v#试图删除对象失败>>> a.v
3#(2)下面可读,可修改,不可删除class A:def __init__(self,v): self.__v = vdef __get(self):return self.__vdef __set(self,v):self.__v = vv = property(__get,__set)def show(self):print self.__vdef show(self):print self.__v>>> a = A(3)>>> a.v3>>> a.v = 4#允许修改>>> a.v4>>> a.show()#属性值对应的私有变量也得到了修改5>>> del a.v#试图删除对象失败#(3)下面可读,可修改,可删除class A:def __init__(self,v): self.__v = vdef __get(self):return self.__vdef __set(self,v):self.__v = vv = property(__get,__set,__del)def show(self):print self.__vdef show(self):print self.__v
阅读全文
0 0
- Python初学之类的狭义属性(@property)
- Python初学之类的方法
- Python学习之类属性
- Python “属性(property)”详解
- Python初学之类的继承和多态
- Python中property属性(特性)的理解
- Python拾遗之类属性和实例属性(一)
- Python拾遗之类属性和实例属性(二)
- Delphi 中的属性property(适合初学delphi)
- Delphi 中的属性property(适合初学delphi)
- Delphi 中的属性property(适合初学delphi)
- Python进阶之“属性(property)”详解
- Python进阶之“属性(property)”详解
- Python进阶之“属性(property)”详解
- Python进阶之“属性(property)”详解
- Python进阶之“属性(property)”详解
- Python进阶之“属性(property)”详解
- Python进阶之“属性(property)”详解
- E
- Resources Hook
- jQuery学习之内容过滤选择器、表单域选中选择器
- MYSQL索引分类介绍
- Hibernate学习笔记2
- Python初学之类的狭义属性(@property)
- POJ2184Cow Exhibition(01背包变形)
- 【已解决】谷歌Chrome浏览器中如何打开或关闭网页的自动翻译功能?
- 微信支付工具类
- 解决VS2013调试ASP.NET中无法调试的问题:当前不会命中断点。在 XXXX.dll 中找到了 XXX.cs 的副本,但是当前源代码与 XXXX.dll 中内置的版本不同。
- 2017.8.2暑假集训第三天
- android studio gradle 离线包 gradle-x.x-all.zip 离线配置
- java操作hbase常用方法总结
- 带指针成员类、析构函数出错、复制/赋值构造函数的注意事项