(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 = 0Use
>>> kent = Homework() >>> # Set >>> kent.grade = 90 >>> # Get >>> kent.grade 90 # Set >>> kent.grade = 110 # Get >>> kent.grade 110As 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 = valueUse.
>>> 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 100Python Is Not Java
Quote:Getters and setters are evil. Evil, evil, I say! Python objects are not Java beans.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.
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 anythingmore 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.