打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
python学习笔记
     @property 可以将python定义的函数“当做”属性访问,从而提供更加友好访问方式,但是有时候setter/getter也是需要的,我们视具体情况吧


请注意以下代码场景:

代码片段1:
Python2.6代码  
  1. class Parrot(object):  
  2.     def __init__(self):  
  3.         self._voltage = 100000  
  4.   
  5.     @property  
  6.     def voltage(self):  
  7.         """Get the current voltage."""  
  8.         return self._voltage  
  9.   
  10. if __name__ == "__main__":  
  11.     # instance  
  12.     p = Parrot()  
  13.     # similarly invoke "getter" via @property  
  14.     print p.voltage  
  15.     # update, similarly invoke "setter"  
  16.     p.voltage = 12  


代码片段2:
Python2.6代码  
  1. class Parrot:  
  2.     def __init__(self):  
  3.         self._voltage = 100000  
  4.   
  5.     @property  
  6.     def voltage(self):  
  7.         """Get the current voltage."""  
  8.         return self._voltage  
  9.   
  10. if __name__ == "__main__":  
  11.     # instance  
  12.     p = Parrot()  
  13.     # similarly invoke "getter" via @property  
  14.     print p.voltage  
  15.     # update, similarly invoke "setter"  
  16.     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__().

同时我们也可以通过以下方法来验证
Python 2.6代码  
  1. class A:  
  2.     pass  

>>type(A)
<type 'classobj'>
Python 2.6代码  
  1. class A(object):  
  2.     pass  

>>type(A)
<type 'type'>

从返回的<type 'classobj'>,<type 'type'>可以看出<type 'type'>是我们需要的object类型(python 3.0 将object类作为默认基类,所以都将返回<type 'type'>)

为了考虑代码的python 版本过渡期的兼容性问题,我觉得应该定义class文件的时候,都应该显式定义object,做为一个好习惯


最后的代码将如下:
Python代码  
  1. class Parrot(object):  
  2.     def __init__(self):  
  3.         self._voltage = 100000  
  4.  
  5.     @property  
  6.     def voltage(self):  
  7.         """Get the current voltage."""  
  8.         return self._voltage  
  9.  
  10.     @voltage.setter  
  11.     def voltage(self, new_value):  
  12.         self._voltage = new_value  
  13.   
  14. if __name__ == "__main__":  
  15.     # instance  
  16.     p = Parrot()  
  17.     # similarly invoke "getter" via @property  
  18.     print p.voltage  
  19.     # update, similarly invoke "setter"  
  20.     p.voltage = 12  


另外,@property是在2.6、3.0新增的,2.5没有该功能。

以上为自己@property经历,我也在学习python中,目前使用的是python 2.6.6 final,很多东西不懂,在此笔记下,也希望对其他同学有帮助

Good luck!

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Python深入学习之对象的属性
python装饰器--@property
Python 延迟初始化(lazy property)
Python集训营45天—Day07 (面向对象编程进阶)
Python 的常用装饰器
Python面试必须要看的15个问题| 编程派 | Coding Python
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服