How to Use the Python delattr() Function?

Python Delattr() Method

Hey, readers! In this article, we will be focusing Python delattr() function.


Getting started with Python delattr() method

In Object Oriented Programming, a Class is an entity that wraps the attributes and the behavior and represents the same using an Object.

The feature of a class does exist in Python as it is an object-oriented language. While creating attributes and defining the behavior, we may come across situations wherein we would want to delete certain attributes of a Python class. This is when Python delattr() comes in the picture.

The delattr() function is used to delete an attribute associated with a particular class.

Syntax:

delattr(object, attribute)

Example:

class Info:
  name = "AskPython"
  lang = "Python"
  site = "Google"
obj = Info()
print(obj.name)
delattr(Info, 'lang')

Output:

AskPython

In the above example, we have created a class Info with the attributes as follows:

  • name = AskPython
  • lang = Python
  • site = google

Further, we have created an object of class ‘Info‘ using the below command:

obj = <classname>()

After creating an object, we have deleted the attribute – ‘lang‘ using delattr() function.


Errors and Exceptions with delattr()

After having deleted an attribute, if we try to access that particular object, the compiler will throw AttributeError.

Example:

class Info:
  name = "AskPython"
  lang = "Python"
  site = "Google"
obj = Info()
delattr(Info, 'lang')
print(obj.lang)

Output:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-5-699f50a1e7c7> in <module>
      5 obj = Info()
      6 delattr(Info, 'lang')
----> 7 print(obj.lang)

AttributeError: 'Info' object has no attribute 'lang'

Deleting attribute with Python del operator

Python del operator can also be used to delete the attribute of class directly without accessing it through an object.

Syntax:

del Class-name.attribute-name

Example:

class Info:
  name = "AskPython"
  lang = "Python"
  site = "Google"
print("Attribute before deletion:\n",Info.lang)
del Info.lang
print("Attribute after deletion:\n",Info.lang)

Output:

Attribute before deletion:
 Python
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-16-9d8ed21690ff> in <module>
      5 print("Attribute before deletion:\n",Info.lang)
      6 del Info.lang
----> 7 print("Attribute after deletion:\n",Info.lang)

AttributeError: type object 'Info' has no attribute 'lang'


Python delattr() method v/s Python del operator

Python delattr() method is more efficient that Python del operator in terms of dynamic deletion of the attributes.

On the other side, Python del operator executes the operation at a higher speed as compared to Python delattr() method.


Conclusion

Thus, in this article, we have understood the deletion of attribute through Python delattr() method and del operator.


References