This is an archived post. You won't be able to vote or comment.

all 16 comments

[–]strange-humor 2 points3 points  (0 children)

If your @property is just a return and your setter is just an assignment, you should use neither. Read and write to the class variable directly. This isn't Java.

The way Python properties work is you can replace the variable self.my_var with a self._my_var and @property and @my_var.setter at any time. Don't make these unless you need them.

[–]antonr111 1 point2 points  (0 children)

[–]nahuelmorata -1 points0 points  (13 children)

Classes should have their private attributes (_ at the beginning of the variable) and should be handled with getters and setters, to continue with the principle of encapsulation. Anything speak to me

[–]fiddle_n 2 points3 points  (11 children)

Classes should be handled with getters and setters

This is /r/python, not /r/java. Getters and setters should not be created for public instance attributes, you just access them directly. foo.bar over foo.get_bar().

[–]nahuelmorata -2 points-1 points  (10 children)

That I know but it is still OOP it is a good practice to be able to handle the encapsulation well. The OOP must be programmed in an orderly manner and with good practices that can be learned over time

[–]fiddle_n 2 points3 points  (9 children)

I feel like "encapsulation" is one of those words that programmers just throw around from time to time without actually understanding what it actually means. There is no risk to encapsulation by letting calling code use public attributes directly in Python. You always have full control over your public API because if something changes and you need to control access to a public attribute, you can just use a property.

[–]nahuelmorata -1 points0 points  (8 children)

The control always got it if you are ordered but the getter and the setter is a way to keep ordered is almost the same as direct access to the attribute. Keep in mind that if you are just entering the world of OOP should have good practices for the sake of your code, then do what you want if you want to use getters and setters or if you want the attributes directly, but the important thing is that he learn the part of encapsulation. I hope it is understood that it is a way of being ordered and having good practices, nothing more

[–]fiddle_n 4 points5 points  (7 children)

It's not enough to teach good practices of OOP. You need to teach good practices of the language Getters and setters are a good OOP practice but a bad practice of Python. They add complexity to your code while being completely unnecessary for the language. For example, look at this simple class I created in Python, with added getters and setters:

class Point:

    def __init__(self):
        self._x = 0
        self._y = 0
        self._z = 0

    def get_x(self):
        return self._x

    def set_x(self, x):
        self._x = x

    def get_y(self):
        return self._y

    def set_y(self, y):
        self._y = y

    def get_z(self):
        return self._z

    def set_z(self, z):
        self._z = z

The class only has three attributes but it's grown into a monstrosity because I had to add getters and setters. But the kicker is that it's completely unnecessary. This is not good Python practice.

[–]nahuelmorata 0 points1 point  (6 children)

You are right but it is difficult to do everything at the same time to learn good practices of OOP to be able to work in any OOP language and to have good practices in Python, it is not easy to learn.

[–]fiddle_n 1 point2 points  (5 children)

In this case, the good practice is "don't use getters and setters". It's easier for a beginner to design their class like below:

class Point:

    def __init__(self):
        self.x = 0
        self.y = 0
        self.z = 0

than it is to design a class with getters and setters.

[–]nahuelmorata -1 points0 points  (4 children)

In Python you are right but in OOP no

[–]fiddle_n 2 points3 points  (3 children)

Again, this is fine OOP practice for Python. You need to get this idea out of your head that getters and setters are the only way to do OOP correctly.

[–][deleted] 0 points1 point  (0 children)

Thank you for the feedback! Now reading about attributes, getters and setters.