In Java, you have to use getters and setters because using public fields gives you no opportunity to go back and change your mind later to using getters and setters. So in Java, you might as well get the chore out of the way up front. In Python, this is silly, because you can start with a normal attribute and change your mind at any time, without affecting any clients of the class. So, don't write getters and setters.
在Python中,我们通常避免使用getter和setter。只要有一个
.speed
属性:请参阅Python is not Java了解动机和要避免的更多陷阱:
当您在获取、设置或删除属性时真正需要执行代码时,请使用
property
。验证、缓存、副作用等都是属性的合理用例。只要在必要时才使用它们。我会选择属性,比如:
在那里你可以改变和得到完全一样的方式。
@property
符号可能更多地用于包装使用Java/C get/set方法、虚拟属性的类,或者如果需要操作输入的值。我经常在GUI类中使用它,在那里,我可以根据widget的属性(类似于自定义事件系统)的变化轻松地强制重绘屏幕。
由于技术上属性在Python中从不私有,因此get/set方法不被视为“pythonic”
当然,您仍然可以使用getter和setter,并且您可以通过在属性前面添加
__
来模拟私有成员:__
“改变”变量名:在定义了__attr
的类classname
之外,__attr
被重命名为_classname__attr
;在上面的示例中,我们可以简单地使用obj1._MyClass__my_attr
。因此__
不鼓励外部使用属性,但它并不像Javaprivate
修饰符那样禁止属性。正如您在问题中提到的,Python中还提供了一些属性。属性的优点是,您可以使用它们来实现返回或设置值的函数,这些值在类之外似乎只是作为普通成员属性访问的。
相关问题 更多 >
编程相关推荐