Python delattr() Function



The Python delattr() function is a built-in function 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 spepcific to an object of the class is termed as intance attributes. Two objects of a class may or may not contain common attributes.

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.

Examples

Now, let's understand how delattr() function works −

Example 1

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 2

The delattr() function deletes the attribute of an 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 3

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 4

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