Does every attribute need advisory methods?

Asked

Viewed 50 times

-1

I am learning POO in Python and I was in doubt whether I should use advisory methods for all the attributes I include in a class. For example, if I create a class with 20 attributes, in which values will be read and written, I should, by convention, create 40 advisory methods (20 Getters + 20 Setters)?

  • 1

    This answers your question? Pythonic way of defining setters and getters, Give emphasis to this passage Using Property (or getters and setters) to simply save the value as it came and return it as it came, makes no sense

  • 1

    I added 2 more questions in the blue box above, read to see that the subject goes much beyond what the answers below say (by the way, none should have been accepted, so simplistic that they are - remember that the idea of the site is to have complete answers that serve tb for future visitors - just see the answers of the links indicated above, what we expect - or at least, what we should look for). Although not in Python, the 2 links I added give a more general overview (although in Python there are particularities explained in the first link - about the "pythonic way")

  • 1

    Complementing, see also here, to understand that Object Orientation is much more complicated than it seems (besides the links already indicated, you can see more here). Finally, another problem of accepting the answer below is that it disregards the specific context of Python (where nothing is really private), and even spreads it over the Internet, helping to spread misinformation (and has another subtlety about the use of the term attribute, see here)

  • 2

    By the way, if you have 20 attributes in the class you may not be delegating responsibilities correctly (and that’s another very important point outside of this topic).

2 answers

0

In fact, no...

Getters and setters are usually used when there is encapsulation assigned to the given variable/method. If there is no encapsulation I see no reason necessary for the treatment of the data.

-3

In POO one of the pillars is the encapsulation of attributes and therefore, I believe that yes.

I’m a beginner tbm, but remembering that the methods gets and setters refer respectively to the access and modification of the attributes of a certain class once placed in private (encapsulated).

Therefore, if you want to access them when creating instances of this object, there is a need to create access methods and modifiers.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.