Python delattr() Function



The Python delattr() function is used for deleting an attribute from an object. Once the specified attribute is deleted, it will throw an error if we try to call it using the class object.

Those attributes that are specific to an object of the class are termed instance attributes. Two objects of a class may or may not contain common attributes.

The delattr() function is one of the built-in functions and does not require any module to import.

Syntax

The syntax of the Python delattr() function is shown below −

delattr(object, attribute)

Parameters

The Python delattr() function accepts two parameters −

  • object − This parameter represents an object from which the attribute is to be removed.

  • attribute − It indicates the attribute which is to be removed from the specified object.

Return Value

The Python delattr() function does not return any values.

delattr() Function Examples

Practice the following examples to understand the use of delattr() function in Python:

Example: Basic Use of delattr() Function

The following example shows the basic use of Python delattr() function. Here, we are deleting an attribute of a specified class and with the help of hasattr() function, we are checking whether the attribute is successfully deleted.

class NewClass:
   newAttr = "Simply Easy Learning"

print("Before deleting the attribute:")    
print("Is attribute exist:", hasattr(NewClass, 'newAttr')) 
delattr(NewClass, 'newAttr')
print("After deleting the attribute:")
print("Is attribute exist:", hasattr(NewClass, 'newAttr'))

When we run above program, it produces following result −

Before deleting the attribute:
Is attribute exist: True
After deleting the attribute:
Is attribute exist: False

Example: Delete Attribute of a Class Object Using delattr()

The delattr() function deletes the attribute of a class object. In this example, we are deleting the newAttr attribute that exist inside the constructor body.

class NewClass:
   def __init__(self):
      self.newAttr = "Simply Easy Learning"

newObj = NewClass()
print("Before deleting the attribute:") 
print("Is attribute exist:", hasattr(newObj, 'newAttr'))  
delattr(newObj, 'newAttr')
print("After deleting the attribute:") 
print("Is attribute exist:", hasattr(newObj, 'newAttr')) 

Following is an output of the above code −

Before deleting the attribute:
Is attribute exist: True
After deleting the attribute:
Is attribute exist: False

Example: Error During Deleting an Object

If we try to delete an attribute which does not exist using the delattr() function, the code will throw AttributeError as illustrated in the below code.

class NewClass:
   pass

newObj = NewClass()
try:
   delattr(newObj, 'newAttr')
except AttributeError as exp:
   print(exp)   

Output of the above code is as follows −

'NewClass' object has no attribute 'newAttr'

Example: Remove Method from a Module Using delattr()

The delattr() function also allows us to remove a method from an existing module of Python. In the code below the math module is imported. Then, with the help of delattr() function, we remove the sqrt() method of the math module.

import math

print("Before deletion:", hasattr(math, 'sqrt'))  
delattr(math, 'sqrt')
print("After deletion:", hasattr(math, 'sqrt'))

Following is an output of the above code −

Before deletion: True
After deletion: False
python_built_in_functions.htm
Advertisements