Python delattr() Function

Deletes an attribute of an object. Note that the attribute is deleted for the instance of that object, not for the class. Other instances (objects) of the same class still retain the attribute.

Syntax

Python
delattr(object, name)

Parameters

ParameterDescription
object Required. The object for which the attribute should be removed
name Required. The name of the attribute to be removed.

Example

Python
class Person:
    def __init__(self):
        self.name = 'Carol'

person1 = Person()
person2 = Person()

print('Deleting the attribute "name" for the instance "person2":')
delattr(person2, 'name')
print('person1 name = ', person1.name)
print('person2 name = ', person2.name)

Output

Deleting the attribute "name" for the instance "person2":
person1 name = Carol
Traceback (most recent call last):
  File "BuiltInFunctions.py", line 11, in <module>
    print('person2 name = ', person2.name)
AttributeError: 'Person' object has no attribute 'name'