Summary: In this programming example, we will learn two different ways to delete properties of an object in Python.

Method 1: Using del

The del operator can be used to delete any property of an Python object.

class Student:
    def __init__(self, name, department):
        self.name = name
        self.department = department

        
obj = Student("Bicky Newton", "IT")

del obj.department  #deletes department property

Method 2: Using delattr()

The delattr() is an inbuilt method in Python that deletes the attribute (property) of the specified object.

Syntax:

delattr(obj, attribute)

Example:

class Student:
    def __init__(self, name, department):
        self.name = name
        self.department = department

        
obj = Student("Bicky Newton", "IT")

delattr(obj, department)  #deletes department property

Both of these methods successfully deletes the object’s property in Python.

Leave a Reply

eight − seven =