Python Forum
@property vs __set__ / __get__ and __setattr__ / __getattr__
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
@property vs __set__ / __get__ and __setattr__ / __getattr__
#1
I am new to Python and trying to get my head around Python OOP. I can see that using separate getter and setter is a bad approach and becomes tedious when there are a large amount of user accessible properties. The "proper" way to do this in Python therefore is to use @property and @propertyname.setter decorators.

While I understand the use of @property and @propertyname.setter, the presence of __set__ / __get__ and __setattr__ / __getattr__ have me scratching my head. Why do these even exist if they are not supposed to be used? Also, why there are two pairs and not just one pair of __set__ / __get__.

Finally, do we use the decorator @property or use method Property()? The method needs one to pass the methods that correspond to the getter, setter and deleter for the property
Reply
#2
(Jun-15-2021, 12:15 PM)okhajut Wrote: Finally, do we use the decorator @property or use method Property()?
It's most common to use @property decorator.
As you mention should not just use getter and setter in Python,if only need simple attribute accesses.
A example.
class Homework:
    def __init__(self):
        self.grade = 0
Use
>>> kent = Homework()
>>> # Set
>>> kent.grade = 90
>>> # Get
>>> kent.grade
90
# Set
>>> kent.grade = 110
# Get
>>> kent.grade
110
As see so do getter and setter work fine here,do not need to write own method for this.
Only if need more that simple attribute access should use property.
Also an of aspect @property decorator is that original implementation can be made backward compatible.
See that the call is just the same,but now can not do 110.
class Homework:
    def __init__(self):
        self._grade = 0

    @property
    def grade(self):
        return self._grade

    @grade.setter
    def grade(self, value):
        if not (0 <= value <= 100):
            raise ValueError('Grade must be between 0 and 100')
        self._grade = value
Use.
>>> kent = Homework()
>>> kent.grade = 90
>>> kent.grade
90
>>> 
>>> kent.grade = 110
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "G:\div_code\my_files\homework.py", line 12, in grade
    raise ValueError('Grade must be between 0 and 100')
ValueError: Grade must be between 0 and 100
Python Is Not Java
Quote:Getters and setters are evil. Evil, evil, I say! Python objects are not Java beans.
Do not write getters and setters.
This is what the ‘property’ built-in is for.
And do not take that to mean that you should write getters and setters, and then wrap them in ‘property’.
That means that until you prove that you need anything more than a simple attribute access, don’t write getters and setters.
They are a waste of CPU time, but more important, they are a waste of programmer time.
Not just for the people writing the code and tests, but for the people who have to read and understand them as well.

In Java, you have to use getters and setters because using public fields gives you no opportunity to go back and change your mind later to using getters and setters.
So in Java, you might as well get the chore out of the way up front.
In Python, this is silly, because you can start with a normal attribute and change your mind at any time, without affecting any clients of the class.
So, don’t write getters and setters.
Can look at his Thread how it should not be done,i was little frustrating that probably a teacher that comes from Java and lean student to do it just then same way in Python.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Subclass initialized property used in parent class method. Is it bad coding practice? saavedra29 5 1,761 Feb-07-2022, 07:29 PM
Last Post: saavedra29
  what if __getattr__() can't find an attribute, either? Skaperen 1 1,459 Oct-08-2021, 08:43 PM
Last Post: Yoriz
  ABC Module and @property decorator, Pythonic Way? muzikman 21 5,646 Aug-18-2021, 06:08 PM
Last Post: muzikman
  Can property getters and setters have additional arguments? pjfarley3 2 3,042 Oct-30-2020, 12:17 AM
Last Post: pjfarley3
  Property price calculation oli_action 4 3,145 Jul-15-2020, 04:27 PM
Last Post: sridhar
  Use of @property decorator ruy 16 6,518 Jun-09-2020, 05:29 PM
Last Post: buran
  Problem adding keys/values to dictionary where keynames = "property" and "value" jasonashaw 1 2,039 Dec-17-2019, 08:00 PM
Last Post: jasonashaw
  strange class property KaliLinux 2 2,347 Nov-25-2019, 04:32 PM
Last Post: KaliLinux
  print all method and property of list object engmoh 4 2,842 Oct-26-2019, 05:33 PM
Last Post: engmoh
  __getattr__ and type hint itaybardugo 0 2,638 Jul-04-2019, 09:50 PM
Last Post: itaybardugo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020