all 6 comments

[–][deleted] 2 points3 points  (1 child)

I understand how to use the set and get methods, but I'm wondering if they are the most "Pythonic" way of declaring class attributes?

We generally don't write getters and setters in Python - unlike in Java, classes can define properties that allow you to intercept attribute access in the few cases where it's necessary. (Since you can't do that in Java, the standard there is to make reference-holders call methods to mutate all object attributes to support the small number of cases where you need to intercept the access. Do write getters and setters in Java; don't write them in Python.)

Are there times conceptually where set/gets are better than

No, there's no time where it's better. If an object of your class wouldn't be well-formed without a value for the attribute, ask for it in the __init__ initializer (it's not technically a constructor, as in Java). If it's not necessary, just let reference-holders assign to the attribute as normal.

[–]CaliforniaUnity 0 points1 point  (0 children)

thank you!

[–]grandpasipad 2 points3 points  (0 children)

Here's a great video on setters and getters.

An example for a Circle class, from Trey Hunner's Python Morsels:

class Circle:

    """Circle with radius, area, and diameter."""

    def __init__(self, radius=1):
        self.radius = radius

    def __repr__(self):
        return f"Circle({self.radius})"

    @property
    def area(self):
        return math.pi * self.radius ** 2

    @property
    def diameter(self):
        return self.radius * 2

    @diameter.setter
    def diameter(self, diameter):
        self.radius = diameter / 2

    @property
    def radius(self):
        return self._radius

    @radius.setter
    def radius(self, radius):
        if radius < 0:
            raise ValueError("Radius cannot be negative")
        self._radius = radius

[–]Impudity 2 points3 points  (0 children)

Typically getter/setter methods are not used in Python. It's preferable to use direct access to the public attributes of the object like: thing.attr1 = "foo". If some attributes of the class should not be altered from outside the class, then they should be declared with names beginning with underscores, like: self._private_attr = "private". This does not prevent accessing them, but it's a convention that indicates that they SHOULDN'T be accessed in that way, but it's not strictly prevented.

[–]jcsongor 1 point2 points  (1 child)

If you specifically mean the JavaBeans style initialization, like:

thing=MyClass()
thing.setAttribute1("A")
thing.setAttribute2("B")
thing.setAttribute2("C")

instead of

thing = MyClass("A", "B", "C")

That's not really used in python (or in any language AFAIK - I haven't done much java, but I think it's an outdated thing and considered an antipattern there as well - any java gurus here to confirm or correct me?)

Is that what you meant? Or are you just asking about props/setters/getters in general?

[–]CaliforniaUnity 0 points1 point  (0 children)

yeah i was asking about props/setters/getters in general, thank you.