The delattr() method is used to delete the named attribute from the object, with the prior permission of the object.
delattr(object, name) The function takes only two parameter: object : from which the name attribute is to be removed. name : of the attribute which is to be removed. The function doesn't returns any value, it just removes the attribute, only if the object allows it.
The Working : Suppose we have a class by name Geek and it has five students as the attribute. So, using the delattr() method, we can remove any one of the attributes.
Students before delattr()-- First = Henry Second = Zack Third = Stephen Fourth = Amy Fifth = Shawn After deleting fith student-- First = Henry Second = Zack Third = Stephen Fourth = Amy
When the execution moves to the last line of the program, i.e., when the fifth attribute is called, the compiler raises an error:
Traceback (most recent call last): File "/home/028e8526d603bccb30e9aeb7ece9e1eb.py", line 25, in print('Fifth = ',names.stu5) AttributeError: 'Geek' object has no attribute 'stu5'
There is another operator in Python that does the similar work as the delattr() method. It is the del operator. Let’s see how it works.
Students before del-- First = Henry Second = Zack Third = Stephen Fourth = Amy Fifth = Shawn After deleting fith student-- First = Henry Second = Zack Third = Stephen Fourth = Amy
The result is the same with an error:
Traceback (most recent call last): File "/home/7c239eef9b897e964108c701f1f94c8a.py", line 26, in print('Fifth = ',names.stu5) AttributeError: 'Geek' object has no attribute 'stu5'
del vs delattr()
- Dynamic deletion : del is more explicit and efficient and delattr() allows dynamic attribute deleting.
- Speed : If the above programs are considered and run then there is a slight difference between the speed of execution. del is slightly faster in comparison to delattr(), depending on the machine.
- byte-code Instructions : del also takes less byte-code instructions in comparison to delattr().
So we conclude the comparison by saying that del is slightly faster than delattr, but when it comes to dynamic deletion of attribute then delattr() has advantage as it is not possible by del operator.
- Reading Python File-Like Objects from C | Python
- Important differences between Python 2.x and Python 3.x with examples
- Python | Merge Python key values to list
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
- Python | Add Logging to Python Libraries
- Python | Add Logging to a Python Script
- Any & All in Python
- Use of min() and max() in Python
- abs() in Python
- Python Set | pop()
- gcd() in Python
- pow() in Python
- set add() in python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.