Curriculum
A read-only property in Python is a property that can only be accessed for reading, but not for writing. This means that the value of the property can be retrieved, but it cannot be changed directly.
In Python, we can create a read-only property by defining a getter method using the @property decorator, without defining a corresponding setter method. This makes the property read-only, since there is no way to set its value directly.
Here is an example of how to define a read-only property in Python:
class Circle:
    def __init__(self, radius):
        self._radius = radius
    
    @property
    def radius(self):
        return self._radius
    
    @property
    def area(self):
        return 3.14 * self._radius ** 2
c = Circle(5)
print(c.radius)    # 5
print(c.area)      # 78.5
c.radius = 7       # AttributeError: can't set attribute
In this example, we define a Circle class with a read-only radius attribute, which is created using the @property decorator. We also define a area attribute, which is calculated based on the value of radius.
We create a Circle object c with a radius of 5, and use the radius and area attributes to retrieve the corresponding values. When we try to set the radius attribute to a new value, we get an AttributeError, since the radius attribute is read-only and cannot be set directly.
Read-only properties can be useful in situations where we want to provide read-only access to an attribute, while keeping its value protected from modification. They can also be used to provide a simple and intuitive interface for working with objects, especially when the attributes require custom logic for access and modification.