Python delattr() Function with Examples

delattr() Function in Python:

The delattr() function removes the specified attribute from the given object.

In Python, there is another operator that performs the same function as the delattr() method and that is the

del operator.

del operator versus delattr () function:

Dynamic deletion: The del function is more explicit and efficient, and the delattr() function allows for dynamic attribute deletion.

Speed: When the programs are considered and executed, there is a slight difference in execution speed. Depending on the machine, del is slightly faster than delattr().

byte-code Instructions: del uses fewer byte-code instructions than delattr ().

So we conclude the comparison by saying that del is slightly faster than delattr, but delattr() has an advantage when it comes to dynamic deletion of attributes, which the del operator does not support.

Syntax:

delattr(object, attribute)

Parameters

object: This is Required. It is an object.

attribute: This is Required. The name of the attribute to be removed

Return Value:

delattr() does not return any outcome (returns None). It only deletes one attribute (if the object allows it).

delattr() Function with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Create a class to say Employdetails.
  • Take a variable and initialize it with some random number(id).
  • Take another variable and initialize it with some random name(ename).
  • Take another variable and initialize it with some random job role(jobrole).
  • Create an object for the given class and store it in a variable.
  • Print the value of the attribute by using the above-declared object.
  • Pass the above class name and attribute name to the delattr() function to delete a given attribute from the class.
  • Print the value of the attribute by using the above-declared object after deleting.
  • The Exit of the Program.

Below is the implementation:

# Create a class say Employdetails.
class Employdetails:
    # Take a variable and initialize it with some random number(id).
    id = 10
    # Take another variable and initialize it with some random name(ename).
    ename = 'virat'
    # Take another variable and initialize it with some random jobrole(jobrole).
    jobrole = 'developer'


# Create an object for the given class and store it in a variable.
classobj = Employdetails()
# Print the value of the attribute by using the above declared object.
print("The attribute is :",classobj.ename)
# Pass the above class name and attribute name to the delattr() function to delete
# given attribute from the class.
delattr(Employdetails, 'ename')
# Print the value of the attribute by using the above declared object after deleting.
print("The given attribute after deleting from class = ",classobj.ename)

Output:

The attribute is : virat
Traceback (most recent call last):
File "jdoodle.py", line 19, in <module>
print("The given attribute after deleting from class = ",classobj.ename)
AttributeError: 'Employdetails' object has no attribute 'ename'

Explanation:

Here first it prints the given attribute 'ename'.
Then delete that attribute from the class 'Employdetails'.
Again try to print the given attribute 'ename'.
Now, it raises an error since the attribute 'ename' got deleted from it.