Property Decorator
Python Property Decorator - @property
Section titled “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.
@propertyproperty() function@propertyclass
You can use the following three decorators to define a property:
decorators- @property: Declares the method as a property.
- @
.setter: Specifies the setter method for a property that sets the value to a property. - @
.deleter: Specifies the delete method as a property that deletes a property.
Declare a Property
Section titled “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`class Student:
def __init__(self, name): self.__name = name
@propertydef 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.
@property``name()``name()private__name``name()``__name
s = Student('Steve')print(s.name) #'Steve's = Student('Steve') print(s.name) #'Steve'Try it
Property Setter
Section titled “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.
name()``name``name``@property-name.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`class Student: def init(self, name): self.__name=name
@propertydef name(self): return self.__name
@name.setter #property-name.setter decoratordef 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.
name()
s = Student('Steve')print(s.name) #'Steve'
s.name = 'Bill'print(s.name) #'Bill'`s = Student(‘Steve’) print(s.name) #‘Steve’
s.name = ‘Bill’ print(s.name) #‘Bill’`Try it
Property Deleter
Section titled “Property Deleter”Use the @property-name.deleter decorator to define the method that deletes a property, as shown below.
@property-name.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): print('Deleting..') del self.__name
std = Student('Steve')del std.nameprint(std.name) #AttributeError`class Student: def init(self, name): self.__name = name
@propertydef name(self): return self.__name
@name.setterdef name(self, value): self.__name=value
@name.deleter #property-name.deleter decoratordef name(self): print('Deleting..') del self.__namestd = Student(‘Steve’)
del std.name
print(std.name) #AttributeError[Try it](/codeeditor?cid=python-3z8uqc3v6) 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. del`