Introduction to property in OPP
The decorator is one of the very important and special kinds of functions in python. It is a kind of function that takes another function as an argument and returns another function with modified behavior. In other words, a decorator is a pattern in python that gives the user an opportunity to add new functions to existing objects without modifying their structure.
The property, usually written as @propertry, is a type of decorator. If we want to use a function like a property or an attribute, we can use @property decorator. It is a built-in function that resides under the property() function. Thus the property() function is applicable when it comes to defining the properties of the python class. The property() method takes the get, set, and delete methods as arguments and returns an object of the property class. It is recommended to use the property decorator instead of the property() method.
Thus the benefit of having the property() function is that there is no need to manually call the property() function to get the properties of the class, yet it makes our work easier.
There are mainly three methods associated with a property in python:
- Python getter - it is used to access the value of the attribute.
- Python setter - it is used to set the value of the attribute.
- Python deleter - it is used to delete the instance attribute.
The property() method returns the function as a property class using these getters, setters, and deleters.
What are getters and setters in Python?
Python getters are the methods used in Object-Oriented Programming (OOPS) that help to access the private attributes from a class while Python setters are the methods used in the OOPS feature which help to set the value to private attributes of a class.
Let us create a class with some methods to understand how the getter and setter work in Python.
# creating the class
class Car:
# initializing
def __init__(self, model):
# private varibale or property in Python
self.__model = model
#getter method to get the property
def get(self):
return self.__model
#setter method to change the value
def set_a(self, model):
self.__model = model
Now we will create a new object and assign a model number for it.
# creating an object
car = Car(2016)
Now, let us assume that we don't know the car's model and we want to get access to its model property. In such case, we can use the python getter method to get access to the property of the model as shown below:
## getting the value of model
print(car.get())
Output:
Now let us assume that the model of the car has been set incorrectly. So, in order to access the model and set it to a new value, we need a Python setter. Let us use a Python setter to set the model to the new value and get it again.
## setting a new value
car.set_a(2019)
## getting the value of model
print(car.get())
Output:
As you can see, the value of the model has been changed now.
Understanding Python deleter
Python deleter method is used to delete the property of an object. Here notice that the python deleter does not delete the property of the class, in fact, it deletes the property of an object. In this section, we will learn how we can use Python deleter to delete the property of an object using various examples. Mainly we will use the following two methods to implement python deleters.
- Python deleter with methods
- Python deleter with decorators
Example-1: Python deleter with methods
Let us again create a simple class that will have setter, getter, and python deleter methods.
# creating the class
class Student:
# initializing
def __init__(self, name):
# private varibale or property in Python
self.__name = name
#getter method to get the property
def get(self):
return self.__name
#setter method to change the value
def set_a(self, name):
self.__name = name
# creating python deleter
def deleter(self):
del self.name
As you can see, we have created a class of Student with setter, getter, and python deleter which deletes the name of the student.
Let us now create an object and specify the name of the student.
# creating an object
student1 = Student("Bashir Alam")
# accessing the name of the student
print(student1.get())
Output:
Now, let us use the python deleter to delete the name of the student. If we will try to access the name of the student after calling python deleter, we will get an error as shown below:
# calling python deleter
student1.deleter()
# printing the name
print(student1.get())
Output:
As you can see, we get an error when we tried to get the name of the student because we used the python deleter to delete the name of the object.
Example-2: Python deleter with decorator
A decorator is a design pattern in Python that allows a user to add new functionality to an existing object without modifying its structure. Decorators are usually called before the definition of a function you want to decorate for example @property.
We will now create a class Student with decorators.
# creating a class
class Student:
def __init__(self, name):
self._name = name
# using decorator for property
@property
def name(self):
return self._name
# using decorators for setter
@name.setter
def name(self, value):
self._name = value
#using decorator for python deleter
@name.deleter
def name(self):
del self._name
As you can see, we have created a class. Now let us first create an object and call the name.
# creating new object
person = Student('Bashir Alam')
# printing the name
print(person.name)
Output:
Now we will call the python deleter and then will try to access the name property again.
# deleting the name
del person.name
# calling name
print(person.name)
Output:
As you can see, we get an error because the name has been deleted using python deleter.
Summary
Python has a deleter method that is used to delete the property of a class. Once the property is deleted, it cannot be accessed using the same instance. In this short article, we discussed how we can use the python deleter to delete the property of an object. We used the python deleter with and without decorators.
Further Reading