del and delattr() in Python

In this tutorial, we are going to discuss a function(method) and an operator that has the same purpose. The first one is delattr() and the second one is del.
These two functions and operators used to remove attributes from the classes if the objects allowed it.

delattr() in Python

The delattr() function is the built-in function(method) in the Python.The main use of the delattr() method is to delete an attribute from the object if the object allows it to do so. It means that to delete an attribute the permission of the object is required.

syntax:delattr(object,name)

delattr() takes two arguments.

  • object: The object in the code from which name attribute is to be removed.
  • name: A string that should be the name of the attribute to be removed from the object.

The code for delattr() is as below:

class Example: 
pro1 = "Pratik"
pro2 = "Rajesh"
pro3 = "Jayu"
pro4 = "Vinod"
pr05 = "Raju"

x = Example 

print('Students before  the use of delattr():') 
print('First = ',x.pro1) 
print('Second = ',x.pro2) 
print('Third = ',x.pro3) 
print('Fourth = ',x.pro4) 
print('Fifth = ',x.pro5) 

# The delattr method 
delattr(Example, 'pro5')

print('After the deletion of fifth student:') 
print('First = ',x.pro1) 
print('Second = ',x.pro2) 
print('Third = ',x.pro3) 
print('Fourth = ',x.pro4) 
# The following statement will raise an error 
print('Fifth = ',x.pro5) 
output:
Students before the use of delattr():
First =  Pratik
Second =  Rajesh
Third =  Jayu
Fourth =  Vinod
Fifth =  Raju
After deleting fifth student:
First =  Pratik
Second =  Rajesh
Third =  Jayu
Fourth =  Vinod
Error:
Traceback (most recent call last):
  File "/home/028e8526d603bccb30e9aeb7ece9e1eb.py", line 25, in 
    print('Fifth = ',x.pro5)
  AttributeError: 'Example' object has no attribute 'pro5'

del in Python

del is the operator in Python which does the same work as the delattr() method does. The following code illustrates the working of the del operator.

Code for the del is as below:

class Example: 
pro1 = "Pratik"
pro2 = "Rajesh"
pro3 = "Jayu"
pro4 = "Vinod"
pro5 = "Raju"

x = Example() 

print('Students before the use of del:') 
print('First = ',x.pro1) 
print('Second = ',x.pro2) 
print('Third = ',x.pro3) 
print('Fourth = ',x.pro4) 
print('Fifth = ',x.pro5) 

# implementation of del operator 
del Example.pro5 

print('After deletion of fith student:') 
print('First = ',x.pro1) 
print('Second = ',x.pro2) 
print('Third = ',x.pro3) 
print('Fourth = ',x.pro4) 
# the following statement will raise an error 
print('Fifth = ',x.pro5) 
output:
Students before the use of del:
First =  Pratik
Second =  Rajesh
Third =  Jayu
Fourth =  Vinod
Fifth =  Raju
After deletion of fifth student:
First =  Pratik
Second =  Rajesh
Third =  Jayu
Fourth =  Vinod
Error:
Traceback (most recent call last):
  File "/home/7c239eef9b897e964108c701f1f94c8a.py", line 26, in 
    print('Fifth = ',x.pro5)
AttributeError: 'Example' object has no attribute 'pro5'

You can also refer to:

Leave a Reply

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