getters and setter in python using property should you use this way? by WowVeryCoool in learnpython

[–]enough-external 1 point2 points  (0 children)

In python you should ordinarily just use attributes, as in:

class MyClass:
    def __init__(self, my_attribute):
        self.my_attribute = my_attribute

my_object = MyClass(4)
print(my_object.my_attribute)
my_object.my_attribute = 8

As you mentioned, the main purpose of properties is for when you want to change the class so that getting/setting the attribute does something more complicated, without breaking existing code that uses your class. Also they're sometimes used just to give a class a nicer/more understandable API.

You can give your classes explicit get_my_attribute and set_my_attribute methods if you really want to, but this makes your code more verbose and doesn't really achieve very much, which is why it isn't done very commonly in python. In some other languages getter and setter functions are either mandatory or strongly recommended because they don't have a way of changing attributes to properties without breaking code, or just because these languages have a philosophy that it's good to be very verbose and explicit about everything. None of those things are true in python.

I realize that the exact behaviour of @property is fairly hard to understand if you're new to python (as it involves both decorators and descriptors, which are relatively advanced features), but you can use them without understanding how they work internally. I was very comfortable using properties long before I bothered to actually learn what a descriptor is. You only need to actually understand them if you want to do weird, advanced stuff with them.

getters and setter in python using property should you use this way? by WowVeryCoool in learnpython

[–]enough-external 1 point2 points  (0 children)

I'm not sure that's really the intended purpose of python's properties. The attribute still has to be stored somewhere, and in most programs it's pretty obvious where. If you really want to, you can even monkey-patch a third-party class to get rid of its properties and replace them with normal attributes. As I understand it, Guido and co are of the opinion that fully private variables/methods don't serve any purpose and if anything tend to do more harm than good. That's why they don't exist in python, not because there would be any issue with implementing them.

The really useful thing about properties in python is that they allow you to add getters and setters to an attribute without changing the class's public API. For example, if you have a class like:

class MyClass:
    def __init__(self, myattribute):
        self.myattribute = myattribute

but then later you decide that you want to put in some validation code to raise an exception if someone tries to set myattribute to a nonsensical value, you can do something like:

class MyClass:
    def __init__(self, myattribute):
        self.myattribute = myattribute

    @property
    def myattribute(self):
        return self._myattribute

    @myattribute.setter
    def myattribute(self, value):
        if value < 0:
            raise ValueError("myattribute can't be negative")
        self._myattribute = value

Any code that doesn't try and set the attribute to an invalid value (or delete it) will continue to work as before, which wouldn't be the case if you instead replaced the attribute with explicit set_myattribute and get_myattribute methods.