Do you use the get/set pattern (in Python)?

后端 未结 8 1987
长情又很酷
长情又很酷 2020-12-12 13:25

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

Why do you use or avoid get/set methods in Py

8条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-12 13:58

    Cool link: Python is not Java :)

    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.

提交回复
热议问题