Python Property Decorator - @property

The @property decorator is a built-in decorator in Python for the property() function. Use @property decorator on any method in the class to use the method as a property.

You can use the following three decorators to define a property:

  • @property: Declares the method as a property.
  • @<property-name>.setter: Specifies the setter method for a property that sets the value to a property.
  • @<property-name>.deleter: Specifies the delete method as a property that deletes a property.

Declare a Property

The following declares the method as a property. This method must return the value of the property.

Example: @property decorator
class Student:

    def __init__(self, name):
        self.__name = name

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

Above, @property decorator applied to the name() method. The name() method returns the private instance attribute value __name. So, we can now use the name() method as a property to get the value of the __name attribute, as shown below.

Example: Access Property decorator
>>> s = Student('Steve')
>>> s.name 
'Steve'

Property Setter

Above, we defined the name() method as a property. We can only access the value of the name property but cannot modify it. To modify the property value, we must define the setter method for the name property using @property-name.setter decorator, as shown below.

Example: Property Setter
class Student:

    def __init__(self, name):
        self.__name=name

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

    @name.setter   #property-name.setter decorator
    def name(self, value):
        self.__name = value

Above, we have two overloads of the name() method. One is for the getter and another is the setter method. The setter method must have the value argument that can be used to assign to the underlying private attribute. Now, we can retrieve and modify the property value, as shown below.

Example: Access Property
>>> s = Student('Steve')
>>> s.name 
'Steve'
>>> s.name = 'Bill'
'Bill'

Property Deleter

Use the @property-name.deleter decorator to define the method that deletes a property, as shown below.

Example: Property Deleter
class Student:
    def __init__(self, name):
        self.__name = name

    @property
    def name(self):
        return self.__name
    
    @name.setter
    def name(self, value):
        self.__name=value
    
    @name.deleter   #property-name.deleter decorator
    def name(self, value):
        print('Deleting..')
        del self.__name

The deleter would be invoked when you delete the property using keyword del, as shown below. Once you delete a property, you cannot access it again using the same instance.

Example: Delete a Property
>>> s = Student('Steve')
>>> del s.name 
Deleting.. 
>>> s.name 
Traceback (most recent call last):                              
File "<pyshell#16>", line 1, in <module>            
    p.name                                                      
File "C:\Python37\test.py", line 6, in name                     
    return self.__name                                          
AttributeError: 'Student' object has no attribute '_Student__name'
Want to check how much you know Python?