I'm switching from Java to Python and am having trouble understanding the @Property
decorator. I realized in OOP languages (such as Java) I don't fully understand the point of mutator methods in the following sense:
Say we have a private variable x
. So we need to make a mutator method
public void setX(int val) {
x = val;
}
Now if x
had been public
then we could have just did x = val
and it would have been easier and there would have been no difference. Now in a more complicated scenario, we could add restrictions, perhaps it doesn't make sense for x to be less than 10
public void setX(int val) {
if(x < 10)
x = 10;
else
x = val;
}
So in a sense is the point of having mutator methods to impose restrictions on the data? So it's to achieve encapsulation? I'm thinking of this because with Python's @Property
(correct me if I'm wrong) you're not calling a mutator method but in effect it works the same way in the sense additional logic can happen before the value is changed.
A corolary question is, if we do have such a simple mutator method as in the first example, should the variable just be made public?