文章[1]的作者提到:

What will we do if we need to control access to x, make it read-only or do something else to it? Won't we have to refactor everything to the getters and setters that we avoided?

意思是:

虽然我们看到@property在控制读写属性,那岂不是直接__init__就好了呢?

不是的,因为__init__是所有的成员都是可以任意操作的。

[2]中的结论有(括号中是翻译):

  1. When to use @property decorator?
    When an attribute is derived from other attributes in the class, so the derived attribute will update whenever the source attributes is changed.(只读的时候用到@property)
  2. How to make a @property?
    Make an attribute as property by defining it as a function and add the @property decorator before the fn definition.(用法是在需要被装饰的函数前面加上@property)
  3. When to define a setter method for the property?
    Typically, if you want to update the source attributes whenever the property is set. It lets you define any other changes as well.("意思是两个属性如果一个需要修改,另外一个根据该属性自动修改,那么前者需要setter,后者仅仅@property')

[3]提到:
By using property() method, we can modify our class and implement the value constraint without any change required to the client code. So that the implementation is backward compatible.(意思是在不修改原有代码的情况下就可以加入新的功能)

个人理解:

@property适用于变量极其多的场景,这个场景下,有些变量需要可写,有些变量需要只读,这个时候__init__无法满足需要,@property应运而生。

我们或许会反驳,如果是所有变量都可写的情况呢?

此时使用__init__也可以实现可读可写的效果啊,还要"@propery以及@xxx.setter"干嘛呢?

确实如此,从读写效果上,此时"@propery以及@xxx.setter"和__init__是一致的,不考虑封装的话,此时使用__init__与"@propery以及@xxx.setter"效果上是一致的。

还有一个好处是,使用@propery的话,我们可以看到右下方的函数名都可以保持一致,

让代码更加清晰,试想:

左侧你读代码还要把代码名字理解一遍,

右侧所有成员函数都是def value,扫一眼就知道都是在服务于value这个参数

附录是上面贴图中左右两侧的代码

#---------------------------------------------------------附录中是[3]中的代码-------------------------------------------------------------------------

不使用[3]

# Python program to explain property() function # Alphabet class
class Alphabet: def __init__(self, value): self._value = value # getting the values def getValue(self): print('Getting value') return self._value # setting the values def setValue(self, value): print('Setting value to ' + value) self._value = value # deleting the values def delValue(self): print('Deleting value') del self._value value = property(getValue, setValue, delValue, ) # passing the value
x = Alphabet('GeeksforGeeks')
print(x.value) x.value = 'GfG'del x.value 

使用[3]

# Python program to explain property()
# function using decorator class Alphabet: def __init__(self, value): self._value = value # getting the values     @propertydef value(self): print('Getting value') return self._value # setting the values     @value.setter def value(self, value): print('Setting value to ' + value) self._value = value # deleting the values @value.deleter def value(self): print('Deleting value') del self._value # passing the value
x = Alphabet('Peter')
print(x.value) x.value = 'Diesel'del x.value 

#------------------------------------------------------------------------------------------------------------------------------------------

[1]What's the point of properties in Python?

[2]Python @Property Explained – How to Use and When? (Full Examples)

[3]Python | property() function

@property的必要性相关推荐

  1. 论文阅读笔记(3):A Nullspace Property for Subspace-Preserving Recovery

    论文阅读笔记(3):保子空间恢复的零空间性质 前言 摘要 1. 简介 2. 准备工作和问题提出 2.1. 符号表示和序言 2.2. 稀疏子空间分类与聚类 3. 保子空间恢复的零空间性质 定义1: 定理 ...

  2. redisson get()数据报错,missing type id property ‘@class’

    redisson get()数据报错: com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Missing type id when ...

  3. Java | kotlin 手动注入bean,解决lateinit property loginService has not been initialized异常

    kotlin.UninitializedPropertyAccessException: lateinit property loginService has not been initialized ...

  4. kotlin Bean加载失败lateinit property has not been initialized

    Application run failed org.springframework.beans.factory.UnsatisfiedDependencyException: Error creat ...

  5. python 创建只读的函数(@property的介绍与使用)

    @property 介绍 @property 是python 中的修饰符,将方法转为属性,可以直接通过方法名来访问,注意访问的时候后面不需要添加小括号,否则会报错,作用是将函数变为只读的,不能修改,否 ...

  6. Android 属性动画(Property Animation) ObjectAnimator的介绍

    先说下属性动画与视图动画的区别: 视图动画系统仅提供为 View 对象添加动画效果的功能,因此,如果您想为非 对象添加动画效果,则必须实现自己的代码才能做到.视图动画系统也存在一些限制,因为它仅公开 ...

  7. Android 属性动画(Property Animation) ValueAnimator 的介绍

    先说下属性动画与视图动画的区别: 视图动画系统仅提供为 View 对象添加动画效果的功能,因此,如果您想为非 对象添加动画效果,则必须实现自己的代码才能做到.视图动画系统也存在一些限制,因为它仅公开 ...

  8. java property异常_Java常见的异常和解决的办法

    Java程序设计中我们经常会遇到异常,遇到异常不要浮躁,找到问题的原因解决是很容易的.我们整理了一些异常和解决的方法供大家参考. 1.java.lang.IllegalStateException: ...

  9. 1-runtime的Method,IMP,Property,ivar

    基础定义 objc-750 的tar包 objc-private.h 定义 typedef struct objc_class *Class; typedef struct objc_object * ...

最新文章

  1. java实现redis缓存_java实现redis缓存功能
  2. 从一道面试题谈谈一线大厂码农应该具备的基本能力
  3. Spring Boot —— YAML配置文件
  4. 免授权版傻瓜式建站系统
  5. Codeforces Zepto Code Rush 2014 -C - Dungeons and Candies
  6. Java Web项目在Linux服务器自动化部署续-整合Bamboo
  7. matlab函数anova,MATLAB进行单因素方差分析-ANOVA
  8. Java静态变量,常量,成员变量,局部变量
  9. 查看和修改MySQL数据库表存储引擎
  10. 领克01、领克02、领克03,百度地图新系统安装第三方软件教程
  11. Excel如何用身份证号提取性别
  12. 毕业一年一点不平凡经历,不平常的感受,有了不平常的自信
  13. mybatis中count(*)与count (*)的区别
  14. 常用html5阅览器,最好的网页浏览器排行,速度最快的四大浏览器分享
  15. 阶乘因式分解(一)/java
  16. C++小课堂:STL中的栈容器(stack)
  17. AWS Boto3 使用介绍(一)
  18. judgement_mna_2016(32位fmt)
  19. google网页翻译使用不了的解决方案
  20. 生物多样性 分类器多样性_优先考虑多样性是谁的工作

热门文章

  1. 当你自定义view用的约束之后,放到其他空间算取frame的时候发现frame里的x,y都是0...
  2. fir.im Weekly - 给 Mac 应用开发者的教程
  3. ZOJ 3720 Magnet Darts (计算几何,概率,判点是否在多边形内)
  4. 转 UIAlertView 不显示、屏幕变灰
  5. sql server2005 无法修改表,超时时间已到 在操作完成之前超时时
  6. The server time zone value ‘XXXXXX’ is unrecognized or represents more than one time zone
  7. vue日期格式化实例
  8. Metric Learning度量学习:**矩阵学习和图学习
  9. 使用QT创建PythonGUI程序
  10. PCL第三方库:Eigen, Flann , Qhull, VTK, Boost简介