您是否使用get/set模式(在Python中)? [英] Do you use the get/set pattern (in Python)?

查看:85
本文介绍了您是否使用get/set模式(在Python中)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用get/set似乎是Java中的一种常见做法(由于各种原因),但我几乎看不到使用此功能的Python代码.

Using get/set seems to be a common practice in Java (for various reasons), but I hardly see Python code that uses this.

为什么要在Python中使用或避免使用get/set方法?

Why do you use or avoid get/set methods in Python?

推荐答案

酷链接:

在Java中,您必须使用getter和setter,因为使用公共字段使您没有机会回头再改变主意,以后再使用getter和setter.因此,在Java中,您还可以避免繁琐的事情.在Python中,这很愚蠢,因为您可以从常规属性开始并随时改变主意,而不会影响该类的任何客户端.因此,请勿编写getter和setter.

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.

这篇关于您是否使用get/set模式(在Python中)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆