2023-02-24

Understanding Property in Python

What is Property in Python

Property is a built-in feature in Python that allows you to manage attributes and methods of a class. It is a powerful tool for encapsulation and data hiding, and is essential for writing efficient and robust code. With property, you can control how your class is accessed and modified, and ensure that any changes to its attributes are properly validated.

Property is implemented using three methods: getter, setter, and deleter. The getter method is used to retrieve the value of a property, the setter method is used to set the value of a property, and the deleter method is used to delete a property.

In addition to the basic getter, setter, and deleter methods, property also supports advanced features such as read-only, write-only, and computed properties. Read-only properties can be accessed but not modified, write-only properties can be modified but not accessed, and computed properties are calculated based on other properties or methods of the class.

Using property in Python is relatively easy. You simply define the property using the @property decorator, and then define the getter, setter, and deleter methods as necessary. With property, you can create classes that are easier to use and understand, and that are less prone to errors and security vulnerabilities.

The Basics of Property: Getters, Setters, and Deleters

In Python, getters, setters, and deleters are special methods that can be used to control access to object properties or attributes. They are also known as property methods. Getters are used to get the value of a property, setters are used to set the value of a property, and deleters are used to delete the value of a property. Here's an example of how to use getters, setters, and deleters in Python:

python
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 not isinstance(value, int):
            raise ValueError("Age must be an integer")
        if value < 0:
            raise ValueError("Age cannot be negative")
        self._age = value

    @age.deleter
    def age(self):
        del self._age

In the example above, we have a Person class that has two attributes, name and age. The @property decorator is used to define getter methods for both attributes. The @name.setter and @age.setter decorators are used to define setter methods for name and age, respectively. Finally, the @age.deleter decorator is used to define a deleter method for age.

To use this class, we can create a new Person object and set its properties using the setter methods:

python
person = Person("John", 30)
person.name = "Jane"
person.age = 35

We can also get the values of the properties using the getter methods:

python
print(person.name)  # Output: Jane
print(person.age)   # Output: 35

If we try to set the age attribute to a non-integer value or a negative value, a ValueError will be raised:

python
person.age = "Thirty"  # Raises ValueError: Age must be an integer
person.age = -5        # Raises ValueError: Age cannot be negative

Finally, we can delete the age attribute using the deleter method:

python
del person.age

This will remove the age attribute from the Person object.

Advanced Property Features: Read-only, Write-only, and Computed Properties

In Python, there are three types of properties: read-only, write-only, and computed. These properties are used to control access to an object's attributes and provide an interface for getting, setting, or computing their values.

A read-only property is an attribute that can be accessed for reading, but not for writing. It is useful when you want to expose an attribute's value without allowing it to be changed directly. To create a read-only property, you can use the @property decorator. Here is an example:

python
class Circle:
    def __init__(self, radius):
        self.radius = radius

    @property
    def area(self):
        return 3.14 * self.radius ** 2

c = Circle(5)
print(c.area)  # Output: 78.5

In this example, the area property is read-only, and it is calculated based on the radius attribute. We cannot set the value of area directly because there is no corresponding setter method.

A write-only property is an attribute that can be set, but not read. It is useful when you want to provide a way to set a value, but you do not want to expose its current value. To create a write-only property, you can define a setter method without a corresponding getter method. Here is an example:

python
class Password:
    def __init__(self, value):
        self._value = value

    @property
    def value(self):
        raise AttributeError('Cannot read the password value')

    @value.setter
    def value(self, new_value):
        if len(new_value) < 8:
            raise ValueError('Password must be at least 8 characters long')
        self._value = new_value

p = Password('password')
p.value = 'newpassword'

In this example, the value property is write-only. We can set its value using the setter method, but we cannot read its value because the getter method raises an AttributeError.

A computed property is an attribute that is not stored directly, but is computed on the fly based on other attributes. It is useful when you want to provide a way to access a value that is derived from other attributes or external data. To create a computed property, you can use the @property decorator and define a method that calculates the value. Here is an example:

python
class Temperature:
    def __init__(self, celsius):
        self.celsius = celsius

    @property
    def fahrenheit(self):
        return self.celsius * 9/5 + 32

t = Temperature(25)
print(t.fahrenheit)  # Output: 77.0

In this example, the fahrenheit property is computed based on the celsius attribute. We can access its value as if it were a normal attribute, but the value is calculated on the fly based on the current value of celsius.

Best Practices for Using Property in Python

When using properties in Python, it's important to follow some best practices to ensure that your code is clear, efficient, and easy to maintain. Here are some best practices for using properties in Python:

  • Use properties to expose object attributes
    Properties should be used to provide access to object attributes, rather than directly exposing the attributes themselves. This allows for greater control over how the attributes are accessed and manipulated, and also allows for validation and data manipulation to be performed when the attribute is accessed or modified.

  • Use the @property decorator
    When defining a property, use the @property decorator to indicate that it is a property. This makes it clear to other developers that the attribute is being accessed through a getter method.

  • Use the @property.setter decorator
    When defining a property that can be set, use the @property.setter decorator to indicate that it has a setter method. This makes it clear to other developers that the attribute can be modified, and also allows for validation and data manipulation to be performed when the attribute is set.

  • Use read-only properties
    When defining a read-only property, make sure that it cannot be modified directly. This can be done by not defining a setter method, or by raising an exception in the setter method.

  • Use computed properties
    Computed properties are useful for calculating attribute values based on other attributes or external data. When defining a computed property, make sure that it is calculated efficiently and accurately, and that it is not called unnecessarily.

References

https://realpython.com/python-property/
https://www.programiz.com/python-programming/property
https://www.programiz.com/python-programming/methods/built-in/property
https://www.freecodecamp.org/news/python-property-decorator/
https://www.geeksforgeeks.org/python-property-function/

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!