Properties

Properties allow us to define special methods (getter, setter, and deleter) that are automatically called when accessing, modifying, or deleting the attributes of an object. Properties provide a way to control the behavior of attribute access and enable you to add validation or additional logic.

Let's take an example of a Person class to understand how properties work:

class Person:
    def __init__(self, name, age):
        self._name = name
        self._age = age

    @property
    def name(self):
        return self._name

    @name.setter
    def name(self, value):
        self._name = value

    @property
    def age(self):
        return self._age

    @age.setter
    def age(self, value):
        if value < 0:
            raise ValueError("Age cannot be negative")
        self._age = value

In this code, we define a Person class with attributes _name and _age. We use the @property decorator to define getter methods for the attributes name and age. We also use the @name.setter and @age.setter decorators to define setter methods for the attributes.

The getter methods, such as name and age, are called automatically when accessing the corresponding attributes. The setter methods, such as name.setter and age.setter, are called automatically when modifying the attributes.

To create an object of the Person class and access or modify its attributes, we can do the following:

person = Person("John", 25)
print(person.name)  # Output: John
print(person.age)  # Output: 25

person.name = "Alice"
person.age = 30

print(person.name)  # Output: Alice
print(person.age)  # Output: 30

In this code, we create an object of the Person class called person. We can access the attributes name and age as if they were regular attributes, but behind the scenes, the getter methods are called. We can also modify the attributes using the setter methods.

Properties allow us to define custom behavior for attribute access and modification. You can add additional logic, perform validation, or control the behavior of attribute access based on specific conditions.

It's important to note that properties provide a way to access and modify attributes in a controlled manner, even though the attributes themselves are prefixed with an underscore _ to indicate that they are intended to be private.