How to Delete Class Objects in Python


In this example we will show how to delete class objects by using the del keyword in Python.

Source Code

class Sum:
    def __init__(self, a, b):
        self.a = a
        self.b = b

    def my_function(self):
        s = self.a + self.b
        print(s)

sum1 = Sum(2, 6)
print(sum1)
del sum1
print(sum1)

Output:

Traceback (most recent call last):
  File "demo.py", line 13, in 
    print(sum1)
NameError: name 'sum1' is not defined
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments