Python Tutorial Python Advanced Python References Python Libraries

Python delattr() Function

The Python delattr() function is used to delete a specified attribute from specified object.


delattr(object, attribute)


object Required. An object
attribute Required. attribute which need to deleted from object.


It raises exception if the required attribute is not found in the object. In the example below, hobby attribute is not defined in the class MyClass. Therefore, when deleting this attribute, causing an exception to occur.

class MyClass:
   name = 'John'
   age = 25
   city = 'London'

delattr(MyClass, 'city')
delattr(MyClass, 'hobby')

The output of the above code will be:

Traceback (most recent call last):
  File "", line 7, in <module>
    delattr(MyClass, 'hobby')
AttributeError: hobby

❮ Python Built-in Functions