Descriptors
In this section, we'll take a look at one last way of altering the semantics of a Python-based syntax, using descriptors. Reading and writing variables is one of the most fundamental aspects of programming. Python's descriptors let us alter how it works.
A descriptor is an object that is stored in a class and controls what it means to get, set, and delete a specific single attribute for instances of that class. If we want that sort of control over multiple attributes, we just add a descriptor to the class for each attribute we want to control.
Using @property to create a descriptor
Python's built-in @property
decorator provides a simple way to create a descriptor. Let's consider an example (refer to the following code example) to illustrate this:

The first prop
method we wrote in the preceding code example tells Python how to figure out the value of an attribute called prop
, which in this case just means fetching it from another attribute and printing the value.
The latter two prop...