How to use Python deleter in a Class?

Python deleter is a method that is used to delete the property of a class. Once the property is deleted, it cannot be accessed using the same instance. One thing to note is that the Python deleter method deletes the property of an object, not the class. To understand Python deleter, first, we need to understand Python classes. In this article, we will learn how we can create a class in Python and will discuss various ways to delete the property using Python deleter.


You may also like


Introduction to OOP in Python

In Python, object-oriented Programming (OOPs) is a programming paradigm that uses objects and classes in programming. It aims to implement real-world entities like inheritance, polymorphisms, encapsulation, etc. in the programming.

A class in OOP is a collection of objects. A class contains the blueprints or the prototype from which the objects are being created. It is a logical entity that contains some attributes and methods. To understand how to declare and understand a class, let us take an example. Let us assume that we created a class for cars that has the property of name and model. Now, using this class we can create different objects/cars with different names and models.

class Car:
 
    # Instance attribute
    def __init__(self, name, model):
        self.name = name
        sel.model = model

Now we can create different objects from the given class.

# creating objects
car1 = Car('black', 2019)
car2 = Car('white', 2020)

Now we can all these objects and print their properties.

#printing property of car1
print('Car 1 is')
print(car1.name)
print(car1.model)
#printing property of car2
print('Car 12 is')
print(car2.name)
print(car2.model)

Output:

how-to-use-python-deleter-calling-object

What is @property in OOP?

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.

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.

Setters and getters in OOP

Getters and setters are used to protect our data, particularly when creating classes. For each instance variable, a getter method returns its value while a setter method sets or updates its value. Given this, getters and setters are also known as accessors and mutators, respectively.

Let us again create a class with a getter and setter.

# creating the class
class Person:
    # 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

Now, we will create an object and use the getter method to get the name of the Person.

# creating an object
person = Person("Jacky")
# getting the method
person.get()

Output:

how-to-use-python-deleter-setter-and-getter

As you can see, the getter is used to get the property of the object. The setter is used to change the value of the property. For example, we will now change the name of a person using the setter method.

## setting a new value
person.set_a('Jonson')
## getting the value of model
print(person.get())

Output:

how-to-use-python-deleter-setter

As you can see, we have changed the value using the setter method.

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

Python deleter with decorators

Decorators provide a simple syntax for calling higher-order functions. By definition, a decorator is a function that takes another function and extends the behavior of the latter function without explicitly modifying it. We use a decorator when you need to change the behavior of a function without modifying the function itself.

Let us create a class with decorators and then we will use the python deleter to delete the property of an object.

# creating a class
class Person:
    
    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

Now, let us create an object and print out the name.

# creating new object
person = Person('Micke')
# printing the name
print(person.name)

Output:

how-to-use-python-deleter-in-oop-deleter

Let us assume that we don’t want the person to have the name property. We use python deleter to delete the name property as shown below:

# deleting the name 
del person.name
# calling name 
print(person.name)

Output:

how-python-deleter-works-deleter

As you can see, we get an error because we have deleted the property using python deleter and then tried to access that property.

Python deleter without decorators

Let us create a class with the getter, setter, and delete methods, rather than having decorators.

# creating the class
class Person:
    # 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

Let us now create an object and print out the name

# creating an object
person = Person("Micke")
# accessing the name of the student
print(person.get())

Output:

how-to-use-python-deleter-in-oop-deleter

As you can see, we get the name. Now let us delete the name property using python deleter.

# calling python deleter
person.deleter()
# printing the name
print(person.get())

Output:

python-deleter-in-oop-deleter

As you can see, we get an error because we have deleted the property using Python deleter.

Summary

Just like the setter method, python has a Python deleter method which is used to delete the property of a class. The syntax of declaring the deleter method decorator is: @property-name. deleter. Once the property is deleted, it cannot be accessed using the same instance. In this article, we discussed how we can use Python deleter in OOP to delete the property of an object using various examples.

Related Articles

1 thought on “How to use Python deleter in a Class?”

  1. Pingback: Sorting a List in Python: A Comprehensive Guide - TechFor-Today

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top