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

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

Why are you using or avoiding get / set methods in Python?

+60
python getter-setter
Apr 05 '10 at 17:26
source share
7 answers

Cold link: Python is not Java :)

In Java, you must use getters and setters because using open fields does not give you the opportunity to go back and change your mind later using getters and setters. Thus, in Java you can also get rid of the problem. In Python, this is stupid because you can start with a normal attribute and change your mind at any time without affecting any class clients. So don't write getters and setters.

+48
Apr 05 '10 at
source share

In python, you can simply access the attribute directly because it is publicly available:

class MyClass(object): def __init__(self): self.my_attribute = 0 my_object = MyClass() my_object.my_attribute = 1 # etc. 

If you want to do something to access or mutate an attribute, you can use the properties :

 class MyClass(object): def __init__(self): self._my_attribute = 0 @property def my_attribute(self): # Do something if you want return self._my_attribute @my_attribute.setter def my_attribute(self, value): # Do something if you want self._my_attribute = value 

It is imperative that the client code remains the same.

+91
Apr 05 '10 at 18:16
source share

Here's what Guido van Rossum says about it at the Founders of Programming

What do you mean by “fighting the tongue”?

Guido: This usually means that they are trying to continue their habits that worked well in another language.

[...] People will turn everything into a class and turn every access into an access method,
where it’s really not wise what to do in Python; you will have more verbose code which is harder to debug and work much slower. Do you know the expression "Can you write FORTRAN in any language?" You can also write Java in any language.

+22
Apr 05 '10 at
source share

No, it's messy. A common way is to use the usual data attribute and replace those that require more complex get / set logic with properties.

+11
Apr 05 '10 at
source share

Your observation is correct. This is not an ordinary Python programming style. Attributes are publicly available, so you just get access (get, set, delete), as with the attributes of any object that has them (and not just classes or instances). It's easy to say when Java programmers learn Python because their Python code looks like Java using Python syntax!

I definitely agree with all previous posters, especially @Maximiliano, a reference to the famous Phillip article and @Max's suggestion that it’s all more complicated than the standard way to set (and get) class and instance attributes using properties (or descriptors for generalizations even more) to configure receiving and setting attributes! (This includes the ability to add your own individual versions of a personal, secure, friendly, or any other policy if you want something other than public.)

As an interesting demonstration in Programming the Basic Python (chapter 13, section 13.16), I came up with an example of using descriptors to store attributes on disk, not in memory! Yes, this is an odd form of persistent storage, but it shows you an example of what is possible!

Here is another article you may find useful: Python: multiple properties, one setter / receiver

+4
Apr 05 '10 at 17:57
source share

The short answer to your question is no, you should use properties when necessary. Ryan Tamyoko gives a long answer in his Getters / Setters / Fuxors article

The main value that needs to be removed from all this is that you want to strive to ensure that each individual line of code has some kind of value or value for the programmer. Programming languages ​​are for people, not machines. If you have code that looks like it does nothing, it’s hard to read, or it seems tedious, then there is a good chance that Python has a language function that allows you to remove it.

+4
Apr 05 '10 at 18:41
source share

Our teacher showed one example in the class explaining when we should use access functions.

 class Woman(Human): def getAge(self): if self.age > 30: return super().getAge() - 10 else: return super().getAge() 
-5
May 05 '15 at 7:09
source share



All Articles