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.
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.
s = Student('Steve')
print(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.
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.
s = Student('Steve')
print(s.name) #'Steve'
s.name = 'Bill'
print(s.name) #'Bill'
Property Deleter
Use the @property-name.deleter
decorator to define the method that deletes a property, as shown below.
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):
print('Deleting..')
del self.__name
std = Student('Steve')
del std.name
print(std.name) #AttributeError
The deleter would be invoked when you delete the property using keyword del
. Once you delete a property, you cannot access it again using the same instance.