Use the del keyword to delete class instance in Python. It’s delete references to the instance, and once they are all gone, the object is reclaimed.
del <Object name you want to delete>
Example of how to Python delete class instance
Simple example code uses the del keyword Delete the object. After that try to run the func() function now, it won’t run because the object is deleted.
class Test(object): def __del__(self): print("Object deleted") def func(self): print("Random function") obj = Test() obj.func() del obj obj.func()
Output: When we try to refer to a deleted object, it raises NameError
Do comment if you have any doubts and suggestions on this Python instance tutorial.
Note: IDE: PyCharm 2021.1.3 (Community Edition)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.