The property() function enables the creation of properties, which act as attribute accessors for classes. 


property(fget, fset, fdel, doc)

The fget argument represents the getter method, used to retrieve the attribute value.

The fset argument signifies the setter method, used to modify the attribute value. If not provided, the property becomes read-only.

The fdel argument specifies the deleter method, allowing the attribute to be deleted. If not defined, the attribute remains undeletable.

The doc argument represents a string that provides documentation (docstring) for the attribute. 

class Circle:
    def __init__(self, radius):
        self._radius = radius
    def get_radius(self):
        return self._radius
    def set_radius(self, value):
        if value > 0:
            self._radius = value
            raise ValueError("Radius must be positive.")
    def area(self):
        return 3.14 * self._radius**2
    radius = property(get_radius, set_radius, doc="Radius of the circle")

my_circle = Circle(5)


my_circle.radius = 7



By using the property() function, we create a property called radius. We provide the get_radius() and set_radius() methods as the getter and setter functions, respectively. The getter retrieves the value of the radius, while the setter allows modification of the radius while enforcing a constraint (in this case, ensuring the radius is positive).  

We can also use the @property as a decorator, as shown below.

class Product:
    def __init__(self, name, price):
        self._name = name
        self._price = price

    def name(self):
        """Get the name of the product."""
        return self._name

    def name(self, value):
        """Set the name of the product."""
        self._name = value

    def name(self):
        """Delete the name of the product."""
        del self._name

    def price(self):
        """Get the price of the product."""
        return self._price

    def price(self, value):
        """Set the price of the product."""
        if value < 0:
            raise ValueError("Price must be a non-negative value.")
        self._price = value

    def price(self):
        """Delete the price of the product."""
        del self._price

p = Product("Widget", 10.99)
print(  # Output: Widget
print(p.price)  # Output: 10.99 = "Gadget"
p.price = 15.99

Importance of using properties

  1. Encapsulation: Properties allow you to encapsulate the internal representation of an attribute within a class. By defining a property, you can control how the attribute is accessed, modified, or deleted. This helps enforce data integrity and protects the internal state of an object.

  2. Controlled Access: Properties provide a controlled interface to access attributes. You can define custom getter and setter methods, which allow you to apply validations, transformations, or calculations on attribute access or modification. This ensures that the attribute is accessed or modified according to your desired rules and constraints.

  3. Code Maintainability: By using properties, you can modify the internal implementation of a class without affecting the external interface. If you later need to add additional logic or change the behavior of an attribute, you can do so in the property methods without affecting the code that uses the class.

  4. Data Validation: Properties enable you to validate the input values before assigning them to attributes. You can add checks, such as range validation, type validation, or business rules, to ensure that only valid data is stored in the attribute.

  5. Computed Attributes: Properties can be used to create computed attributes. These are attributes that are not stored directly but are calculated on-the-fly based on other attributes or external factors. Computed attributes can provide dynamic information or perform calculations when accessed.

  6. Code Readability: Using properties can make your code more readable and self-explanatory. By accessing attributes through properties, you provide a clear and consistent interface to work with the class, making it easier for other developers to understand and use your code.

  7. Backward Compatibility: Properties can be used as a way to transition from direct attribute access to property access without breaking existing code. You can start by exposing attributes directly and later convert them to properties if you need to add additional logic or validation.