Python @property
 decorator is a built-in function that allows us to define methods that can be accessed like an attribute.
This can be useful when we want to add some additional behavior to the getting or setting of an attribute, or when we want to define a method that is more readable and easier to use than a traditional method.
Before we learn about @property
, make sure you know about Python Decorator and Object-Oriented Programming in Python.
Python @Property Getter
In Python, a getter is a method that is used to retrieve the value of an attribute. It is defined using the @property
decorator, which allows the method to be accessed like an attribute.
Let’s see an example,
class Student:
def __init__(self, first_name):
self.first_name = first_name
# define getter method
@property
def get_name(self):
return self.first_name
# create a new Student object
student = Student("Monica")
# access the first name using data property
print(student.first_name) # Monica
# access the first name using getter property
print(student.get_name) # Monica
Output
Monica Monica
In the above example, we have created the getter method get_name()
using the @property
decorator to access get_name()
like an attribute.
As we can see when accessing the getter value, we access the value as a property,
print(student.get_name)
When we try to access the value as a method, an error occurs.
print(student.get_name()) # error
Python @Property Setter
In Python, a setter is a method that is used to set the value of an attribute.
It is defined using the @<property_name>.setter
decorator,
<property_name>
name of the property that the setter is for.
Let’s see an example,
class Person:
def __init__(self, name, age):
self._name = name
self._age = age
# Define a "name" getter
@property
def name(self):
return self._name
# Define a "name" setter
@name.setter
def name(self, value):
self._name = value
# Create a new Person object
person = Person("Alice", 30)
# Access and set the "name" attribute using the getter and setter
print(person.name) # "Alice"
person.name = "Bob"
print(person.name) # "Bob"
Output
Alice Bob
In the above example, the Person class has two methods decorated with the @property
decorator: name
and name.setter
.
The @name.setter
decorator is a way to define a method as a “setter” for a class attribute.
When this method is called, it allows us to set the value of the attribute. In this case, the name
setter method takes a value as an argument and sets the value of the _name
attribute to this value.
Python Property Deleter
A deleter in Python is a method that is used to delete a class attribute.
It is defined using the @<property_name>.deleter
decorator,
<property_name>
– name of the class attribute to delete.
Let’s see an example,
class Person:
def __init__(self, name, age):
self._name = name
self._age = age
# define a "name" getter
@property
def name(self):
return self._name
# define a "name" deleter
@name.deleter
def name(self):
del self._name
# create a new Person object
person = Person("Alice", 30)
# access, set, and delete the "name" attribute
# using the getter, setter, and deleter
print(person.name) # "Alice"
del person.name
print(person.name) # AttributeError
Output
Alice Traceback (most recent call last): File "<string>", line 29, in <module> File "<string>", line 9, in name AttributeError: 'Person' object has no attribute '_name'
In the above example, the @name.deleter
decorator is used to delete the name attribute.
The deleter method del self._name
deletes the _name
attribute from the object when it is called.
