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.

Syntax

delattr(object, attribute)

Parameters

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

Example:

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 "Main.py", line 7, in <module>
    delattr(MyClass, 'hobby')
AttributeError: hobby

❮ Python Built-in Functions