delattr() and del() in Python
Last Updated :
03 Oct, 2022
In this article, we are going to see delattr() and del() functions in Python
delattr() in Python
The delattr() method is used to delete the named attribute from the object, with the prior permission of the object.
Syntax:
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.
Return: The function doesn't return any value.
The Working : Suppose we have a class by named Geek, and it has five students as the attribute. So, using the delattr() method, we can remove any one of the attributes.
Python3
class Geek:
domain = "geeksforgeeks.org"
if __name__ == '__main__':
geeks = Geek()
print("Before deleting domain attribute from geeks object:")
print(geeks.domain)
delattr(geeks, "domain")
print("After deleting domain attribute from geeks object:")
# this will raise AttributeError if we try to access 'domain' attribute
print(geeks.domain)
Output:
Traceback (most recent call last):
File "02d61301-4399-4354-b3c3-86641ba21460.py", line 9, in <module>
delattr(geeks, "domain")
AttributeError: domain
Explanation: The object of the class Geek is having one attribute named domain. If we use delattr() function on the geeks object of the Geeks class for the domain attribute, then it'll no more have the attribute domain. In that case, if we want to access the domain attribute, it will raise an AttributeError.
del operator in Python
There is another operator in Python that does the similar work as the delattr() method. It is the del operator. del operator makes some variable ready for garbage collection, if there is no reference of the variable in the later part of the program control flow.
Python3
class Geek:
domain = "geeksforgeeks.org"
if __name__ == '__main__':
geeks = Geek()
print("Before deleting domain attribute from geeks object:")
print(geeks.domain)
# using del operator
del geeks.domain
print("After deleting domain attribute from geeks object:")
# this will raise AttributeError if we try to access 'domain' attribute
print(geeks.domain)
Output:
Before deleting domain attribute from geeks object:
geeksforgeeks.org
Traceback (most recent call last):
File "792e8f29-bcdc-4756-abc2-72b03b77540d.py", line 10, in <module>
del geeks.domain
AttributeError: domain
delattr() and del() in Python
- 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 attributes then delattr() has an advantage as it is not possible by del operator.
Similar Reads
Python delattr() Function In Python, the delattr() function is used to delete an attribute from an object. In this article, we will learn about the Python delattr() function. Python delattr() Syntaxdelattr (object, name) Parameters: Object: An object from which we want to delete the attribute name: The name of the attribute
3 min read
Python : __delete__ vs __del__ Both __delete__ and __del__ are dunder or magic methods in Python. Dunder or magic methods in Python are the methods having two prefix and suffix underscores in the method name. Dunder here means âDouble Under (Underscores)â. These are commonly used for operator overloading. __del__ __del__ is a des
2 min read
Destructors in Python Constructors in PythonDestructors are called when an object gets destroyed. In Python, destructors are not needed as much as in C++ because Python has a garbage collector that handles memory management automatically. The __del__() method is a known as a destructor method in Python. It is called when
7 min read
numpy.delete() in Python The numpy.delete() function returns a new array with the deletion of sub-arrays along with the mentioned axis.  Syntax: numpy.delete(array, object, axis = None) Parameters : array : [array_like]Input array. object : [int, array of ints]Sub-array to delete axis : Axis along which we want to delete
3 min read
Python del keyword The del keyword in Python is used to delete objects like variables, lists, dictionary entries, or slices of a list. Since everything in Python is an object, del helps remove references to these objects and can free up memorydel Keyword removes the reference to an object. If that object has no other
2 min read