Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The @property decorator is a built-in Python class that implements the descriptor protocol (__get__, __set__, and __delete__) to transform a class method into an attribute. When applied, it binds the decorated method to the property’s internal getter, allowing the method to be accessed via standard dot notation without invocation parentheses.

Syntax and Structure

The property decorator ecosystem consists of three distinct decorators, all of which must share the exact same method name:
class Entity:
    def __init__(self):
        self._attribute = None

    @property
    def attribute(self):
        # Acts as the getter (fget)
        return self._attribute

    @attribute.setter
    def attribute(self, value):
        # Acts as the setter (fset)
        self._attribute = value

    @attribute.deleter
    def attribute(self):
        # Acts as the deleter (fdel)
        del self._attribute

Mechanical Breakdown

Under the hood, @property is not a standard function decorator, but a built-in type. The decorator syntax is syntactic sugar for instantiating the property class, which possesses the following signature:
property(fget=None, fset=None, fdel=None, doc=None)
  1. @property (Getter): Decorating a method with @property creates a new property object, passing the decorated method as the fget argument. If only this decorator is used, the resulting attribute is read-only.
  2. @<name>.setter: The property object exposes a .setter() method. This method acts as a decorator that returns a new copy of the property object, retaining the existing fget while populating the fset argument with the newly decorated method.
  3. @<name>.deleter: Similarly, the .deleter() method returns a new property object with the fdel argument populated by the decorated method.

Functional Equivalence

To understand the decorator’s internal routing, the decorator syntax is functionally identical to explicitly assigning the property() class to a class-level variable:
class Entity:
    def __init__(self):
        self._attribute = None

    def get_attribute(self):
        return self._attribute

    def set_attribute(self, value):
        self._attribute = value

    def del_attribute(self):
        del self._attribute

    # Explicit descriptor instantiation
    attribute = property(fget=get_attribute, fset=set_attribute, fdel=del_attribute)

Descriptor Protocol Routing

When an instance accesses instance.attribute, Python’s attribute lookup mechanism (__getattribute__) detects that attribute is a descriptor (because the property object implements __get__). Instead of returning the property object itself in memory, Python intercepts the lookup and invokes the descriptor’s __get__ method, which executes the function bound to fget. The same routing applies to assignment operations, which trigger __set__ and route to fset, and del operations, which trigger __delete__ and route to fdel.
Master Python with Deep Grasping Methodology!Learn More