Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

delattr()

Requests deletion of the named attribute through the type’s `__delattr__` protocol.

Version: Python 3.14; repository image, production runtime not attested

Syntax

delattr(object, name, /)

Requests deletion of the named attribute through the type’s __delattr__ protocol.

Deleting an absent instance attribute normally raises AttributeError, even if a previous deletion succeeded.

Deleting the instance attribute size returns None. In the displayed class there is no inherited size, so hasattr then returns False and a second deletion raises AttributeError. A class attribute with the same name would instead become visible through the instance after deletion. A class can customize deletion through __delattr__.

class Box:
    size = 3
b = Box()
b.size = 7
assert delattr(b, "size") is None and b.size == 3

Examples

class Box:
    pass

box = Box()
box.size = 3
delattr(box, "size")
print(hasattr(box, "size"))
try:
    delattr(box, "size")
except AttributeError as error:
    print(type(error).__name__)

Result

False
AttributeError

Deleting the instance attribute size returns None. In the displayed class there is no inherited size, so hasattr then returns False and a second deletion raises AttributeError. A class attribute with the same name would instead become visible through the instance after deletion. A class can customize deletion through `__delattr__`.

Sources