python学习笔记 - @property
来源:互联网 发布:企业管理优化方案 编辑:程序博客网 时间:2024/06/07 05:36
- 博客分类:
- python
- http://joy2everyone.iteye.com/blog/910950
Python
@property 可以将python定义的函数“当做”属性访问,从而提供更加友好访问方式,但是有时候setter/getter也是需要的,我们视具体情况吧
请注意以下代码场景:
代码片段1:
代码片段2:
代码1、2的区别在于
class Parrot(object):
在python2.6下,分别运行测试
片段1:将会提示一个预期的错误信息 AttributeError: can't set attribute
片段2:正确运行
参考python2.6文档,@property将提供一个ready-only property,以上代码没有提供对应的@voltage.setter,按理说片段2代码将提示运行错误,在python2.6文档中,我们可以找到以下信息:
BIF:
property([fget[, fset[, fdel[, doc]]]])
Return a property attribute for new-style classes (classes that derive from object).
原来在python2.6下,内置类型 object 并不是默认的基类,如果在定义类时,没有明确说明的话(代码片段2),我们定义的Parrot(代码片段2)将不会继承object
而object类正好提供了我们需要的@property功能,在文档中我们可以查到如下信息:
new-style class
Any class which inherits from object. This includes all built-in types like list and dict. Only new-style classes can use Python's newer, versatile features like __slots__, descriptors, properties, and __getattribute__().
同时我们也可以通过以下方法来验证
>>type(A)
<type 'classobj'>
>>type(A)
<type 'type'>
从返回的<type 'classobj'>,<type 'type'>可以看出<type 'type'>是我们需要的object类型(python 3.0 将object类作为默认基类,所以都将返回<type 'type'>)
为了考虑代码的python 版本过渡期的兼容性问题,我觉得应该定义class文件的时候,都应该显式定义object,做为一个好习惯
最后的代码将如下:
另外,@property是在2.6、3.0新增的,2.5没有该功能。
以上为自己@property经历,我也在学习python中,目前使用的是python 2.6.6 final,很多东西不懂,在此笔记下,也希望对其他同学有帮助
Good luck!
请注意以下代码场景:
代码片段1:
- class Parrot(object):
- def __init__(self):
- self._voltage = 100000
- @property
- def voltage(self):
- """Get the current voltage."""
- return self._voltage
- if __name__ == "__main__":
- # instance
- p = Parrot()
- # similarly invoke "getter" via @property
- print p.voltage
- # update, similarly invoke "setter"
- p.voltage = 12
代码片段2:
- class Parrot:
- def __init__(self):
- self._voltage = 100000
- @property
- def voltage(self):
- """Get the current voltage."""
- return self._voltage
- if __name__ == "__main__":
- # instance
- p = Parrot()
- # similarly invoke "getter" via @property
- print p.voltage
- # update, similarly invoke "setter"
- p.voltage = 12
代码1、2的区别在于
class Parrot(object):
在python2.6下,分别运行测试
片段1:将会提示一个预期的错误信息 AttributeError: can't set attribute
片段2:正确运行
参考python2.6文档,@property将提供一个ready-only property,以上代码没有提供对应的@voltage.setter,按理说片段2代码将提示运行错误,在python2.6文档中,我们可以找到以下信息:
BIF:
property([fget[, fset[, fdel[, doc]]]])
Return a property attribute for new-style classes (classes that derive from object).
原来在python2.6下,内置类型 object 并不是默认的基类,如果在定义类时,没有明确说明的话(代码片段2),我们定义的Parrot(代码片段2)将不会继承object
而object类正好提供了我们需要的@property功能,在文档中我们可以查到如下信息:
new-style class
Any class which inherits from object. This includes all built-in types like list and dict. Only new-style classes can use Python's newer, versatile features like __slots__, descriptors, properties, and __getattribute__().
同时我们也可以通过以下方法来验证
- class A:
- pass
>>type(A)
<type 'classobj'>
- class A(object):
- pass
>>type(A)
<type 'type'>
从返回的<type 'classobj'>,<type 'type'>可以看出<type 'type'>是我们需要的object类型(python 3.0 将object类作为默认基类,所以都将返回<type 'type'>)
为了考虑代码的python 版本过渡期的兼容性问题,我觉得应该定义class文件的时候,都应该显式定义object,做为一个好习惯
最后的代码将如下:
- class Parrot(object):
- def __init__(self):
- self._voltage = 100000
- @property
- def voltage(self):
- """Get the current voltage."""
- return self._voltage
- @voltage.setter
- def voltage(self, new_value):
- self._voltage = new_value
- if __name__ == "__main__":
- # instance
- p = Parrot()
- # similarly invoke "getter" via @property
- print p.voltage
- # update, similarly invoke "setter"
- p.voltage = 12
另外,@property是在2.6、3.0新增的,2.5没有该功能。
以上为自己@property经历,我也在学习python中,目前使用的是python 2.6.6 final,很多东西不懂,在此笔记下,也希望对其他同学有帮助
Good luck!
0 0
- python学习笔记 - @property
- python学习笔记 - @property
- python学习笔记 - @property
- Python 学习笔记property
- python学习笔记 使用@property
- Python学习笔记——Property用法
- python学习-property
- Python模块学习--property
- python学习03 --@property
- python3学习笔记:@property
- ios 学习笔记之property
- python核心编程学习笔记-2016-08-13-05-property()函数
- python学习笔记(21)--类的详解3-属性函数(@property)
- 【Python】学习笔记——-8.2、面向对象高级编程:2.使用@property
- python学习笔记2:面向对象的高级特性2 @property
- python学习——使用@property
- python -- property()/@property
- WTL 学习笔记 -- Property Sheets and Wizards
- mount过程分析之四(vfs_kern_mount->mount_fs->xfs_fs_mount)
- WebGrid Enterprise免费下载
- java锁的种类以及辨析
- linux中的netcat命令 -- 网络中的瑞士军刀
- poj2528 Mayor's posters,优先队列
- python学习笔记 - @property
- ACM博客
- glib库头文件找不着 glib.h/glibconfig.h: no such file or directory
- C语言
- -[MTA4QCStore closeDB] in TencentOpenAPI(MTA4QCStore.o) 忘记加入库lisqlite.framework
- ArcMap——简介及操作地图
- ava内存结构探析 加强
- php-error_reporting
- Android :关于 【Cursor】 类的介绍