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 = "w3wiki.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:
w3wiki.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

In this article, we are going to see delattr() and del() functions in Python

Similar Reads

delattr() in Python

The delattr() method is used to delete the named attribute from the object, with the prior permission of the object....

del operator in Python

...

delattr() and del() 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....