ScanSkill

delattr

This delattr() function deletes the named attribute of an object.

Syntax

delattr(object, name)

Here,

  • object: Required. An object whose attribute is going to be deleted.
  • name: Required. Must be a string. Name of the attribute to delete.

Examples

>>> class Class1:
...     def __init__(self, x=0):
...         self.x = x
...
>>> f = Class1(100)
>>> f.x
100
>>> delattr(f, 'x')
>>> f.x
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
AttributeError: Class1 instance has no attribute 'x'

Here, the named attribute x is deleted or removed from the class Class1.