http://www.python-course.eu/python3_properties.php

Our new class means breaking the interface. The attribute x is not available anymore. That's why in Java e.g. people are recommended to use only private attributes with getters and setters, so that they can change the implementation without having to change the interface.

But Python offers a solution to this problem. The solution is called properties!

The class with a property looks like this:

class P:

    def __init__(self,x):
self.x = x @property
def x(self):
return self.__x @x.setter
def x(self, x):
if x < 0:
self.__x = 0
elif x > 1000:
self.__x = 1000
else:
self.__x = x

A method which is used for getting a value is decorated with "@property", i.e. we put this line directly in front of the header. The method which has to function as the setter is decorated with "@x.setter". If the function had been called "f", we would have to decorate it with "@f.setter".

Two things are noteworthy: We just put the code line "self.x = x" in the init method and the property method x is used to check the limits of the values. The second interesting thing is that we wrote "two" methods with the same name and a different number of parameters "def x(self)" and "def x(self,x)". We have learned in a previous chapter of our course that this is not possible. It works here due to the decorating:

>>> from p import P
>>> p1 = P(1001)
>>> p1.x
1000
>>> p1.x = -12
>>> p1.x
0
>>>

Alternatively, we could have used a different syntax without decorators to define the property. As you can see, the code is definitely less elegant and we have to make sure that we use the getter function in the init method again:

class P:

    def __init__(self,x):
self.set_x(x) def get_x(self):
return self.__x def set_x(self, x):
if x < 0:
self.__x = 0
elif x > 1000:
self.__x = 1000
else:
self.__x = x x = property(get_x, set_x)

There is still another problem in the most recent version. We have now two ways to access or change the value of x: Either by using "p1.x = 42" or by "p1.set_x(42)". This way we are violating one of the fundamentals of Python: "There should be one-- and preferably only one --obvious way to do it."

[REPRINT]Properties vs. Getters and Setters的更多相关文章

  1. 【外文翻译】 为什么我要写 getters 和setters

    原文作者: Shamik Mitra 原文链接:https://dzone.com/articles/why-should-i-write-getters-and-setters 当我开始我的java ...

  2. 为什么要使用getters和setters/访问器?

    Why use getters and setters/accessors? 实际上会有很多人问这个问题....尤其是它成为Coding Style中一部分的时候. 文章出自LBushkin的回答 T ...

  3. JavaBean的getters和setters方法自动生成

    xgClass.java文件: public class XgClass { private String ccCityDerate1000Num; } 添加getter/setter方法: 在代码区 ...

  4. use getters and setters Learning PHP Design Patterns

    w Learning PHP Design Patterns Much of what passes as OOP misuses getters and setters, and making ac ...

  5. Mongoose 预定义模式修饰符 Getters 与 Setters 自定义修饰符

    mongoose 预定义模式修饰符 mongoose 提供的预定义模式修饰符,可以对我们增加的数据进行一些格式化,主要有:lowercase.uppercase .trim,这里不一一演示,对trim ...

  6. Java Reflection - Getters and Setters

    原文链接:http://tutorials.jenkov.com/java-reflection/getters-setters.html 通过使用 Java 反射,我们能够在程序执行时观察 clas ...

  7. Manage, Administrate and Monitor GlassFish v3 from Java code usingAMX &amp; JMX

    http://kalali.me/manage-administrate-and-monitor-glassfish-v3-from-java-code-using-amx-jmx/ Manage, ...

  8. 面试阿里,美团,京东都会被问到的Spring ,从基础到源码帮你全搞定

    1 前言 Spring是一个轻量级开源框架,它是为了解决企业应用开发的复杂性而创建的.框架的主要优势之一就是其分层架构,分层架构允许使用者选择使用哪一个组件,同时为 J2EE 应用程序开发提供集成的框 ...

  9. Kotlin Reference (九) Properties and Fields

    most from reference 声明属性 Koltin的类都有属性,这些属性可以声明为可变的,使用var关键字或用val关键字生声明不可变属性. class Address { var nam ...

随机推荐

  1. JQuery插件 aos.js

    简介: aos.js是一款效果超赞的页面滚动元素动画jQuery动画库插件.该动画库可以在页面滚动时提供28种不同的元素动画效果,以及多种easing效果.在页面往回滚动时,元素会恢复到原来的状态. ...

  2. 让DOM元素自动滚到视野内ScrollIntoView

    概述 项目中需要把一个DOM元素自动滚动到视野内,百思不得其解,最后再element库里面发现了这个方法,记录下来供以后开发时参考,相信对其他人也有用. 参考资料:element scroll-int ...

  3. Delphi XE2 之 FireMonkey 入门(41) - 控件基础: TListBox

    Delphi XE2 之 FireMonkey 入门(41) - 控件基础: TListBox TScrollBox -> TCustomListBox -> TListBox; 其元素项 ...

  4. 以非root身份安装Python的Module或者Package以及pip安装指定路径

    因为要远程访问公司的服务器,没有sudo的权限,所以在安装python的一些包的时候就不能安去默认路径了(比如以/usr/local/lib/为prefix的路径). 一般来讲用easy_instal ...

  5. kafak学习(一)

    发布与订阅消息系统. 数据(消息)的发送者不会直接把消息发送给接受者,这是发布与订阅消息系统的一个特点.发布者以某种方式对消息进行分类,接受者订阅他们,以便接受特定类型的消息.发布与订阅系统一般会有一 ...

  6. 深入理解java:2.1. volatile的使用及其原理

    引言 在多线程并发编程中synchronized和Volatile都扮演着重要的角色,Volatile是轻量级的synchronized,它在多处理器开发中保证了共享变量的“可见性”. 可见性的意思是 ...

  7. eclipse sts 快捷键

    1. ctrl + m 全屏/取消全屏 2.快速切换上面标签 ctrl + pageup/pagedown   : 标签左右切换 ctrl + < > : 标签前后访问处切换 New Ja ...

  8. docker之配置TensorFlow的运行环境

    Docker是一种 操作系统层面的虚拟化技术,类似于传统的虚拟机.传统虚拟机技术是虚拟出一套硬件后,在其上运行一个完整操作系统,在该系统上再运行所需应用进程:而容器内的应用进程直接运行于宿主的内核,容 ...

  9. 小白用Mac

    老话说的好,“最近老板发我一个Mac,但是不会用,嘎嘎嘎嘎” 1.安装软件 安装 Homebrew Homebrew:使用 Homebrew 安装 Apple 没有预装但 你需要的东西,尤其是非界面管 ...

  10. JS中同步与异步

    不讲过多定义,举两个例子说明下 例一: console.log(100); setTimeout(function(){ console.log(200); },1000); console.log( ...