In Python, you can access and modify the attributes of an object directly, using dot notation.
However, it is generally a good object-oriented programming practice to access and modify the attributes of an object through their getters, setters, and deleters, rather than directly using dot notation. This is because using getters, setters, and deleters can give you more control over how the attributes are accessed and modified, and can make your code more readable and easier to understand.
For example, the following example defines a setter method for the attribute _score to limit the range of its value:
works as expected. However, the above implementation seems not elegant enough.
It would be better if we can modify the attribute like a normal attribute using dot notation but still has the limitations, rather than having to call the setter method like a function.
This is why Python provides a built-in decorator named @propery. Using it, we can modify attributes using dot notation directly. It will improve the readability and elegance of our code.
Now, let’s change the previous program a bit: