Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
No results
  • About AskPython
  • Blog
  • Contact Me
  • Free Online Developer Tools Hub
  • Home
  • Meet the Team
  • Privacy Policy
  • Python
  • Python Course for Beginners Online FREE
  • Python Tutorial – Mega Index
  • Terms and Conditions

🚀 Supercharge your YouTube channel's growth with AI.

Try YTGrowAI Free
  • Home
  • Blog
  • About AskPython
  • Python Course
  • Start Here

How to Use the Python delattr() Function?

  • Safa MulaniSafa Mulani
  • May 25, 2020
  • Built-in Methods
How to Use the Python delattr() Function?

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

  • Python delattr() method — Documentation
  • Python deattr() v/s Python del operator — JournalDev
Safa Mulani
Safa Mulani

An enthusiast in every forthcoming wonders!

Articles: 189
Understanding Python Numpy log
Previous Post Understanding Python Numpy log
Next Post Tkinter TreeView Widget
Tkinter TreeView Widget

Related Posts

Print Function Python: Arguments and Output

Print Function Python: Arguments and Output

  • November 28, 2026
How to Use Input Function in Python

How to Use Input Function in Python

  • November 28, 2026
How to Open Files in Python

How to Open Files in Python

  • November 23, 2026

Partner Websites

  • GoLangDocs
  • LinuxForDevices
  • CodeForGeek

Popular Categories

  • Python Programming
  • Python Modules
  • OOPS in Python
  • Python Programming Examples
  • Strings in Python

Hall of Fame

  • Python Programming
  • OOPS in Python
  • Creating Python Modules
  • Args and Kwargs in Python
  • Python Dictionary (Dict) Tutorial
  • Python List – 15 Things You MUST Know
  • Python Set – Things You MUST Know
  • Python String Functions: Methods by Task

Important Links

  • Home
  • Python Course
  • Start Here
  • Blog
  • About AskPython
Latest posts
  • np.linalg.eig: Eigenvalues and Eigenvectors
  • What Does %s Mean in a Python Format String?
  • How to Use np.sin() in NumPy
  • cv2.imread: Load Images with OpenCV
  • pd.read_json: Read JSON Into a DataFrame

Copyright © 2026 - AskPython. All rights reserved.