How to Delete Properties On Class Objects in Python


In this example, we will learn how to delete properties on 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.a)
del sum1.a
print(sum1.a)

Output:

2
Traceback (most recent call last):
  File "demo.py", line 13, in 
    print(sum1.a)
AttributeError: 'Sum' object has no attribute 'a'
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments