CKTIS / Python
delattr()
Remove o atributo indicado segundo as regras de `__delattr__` e devolve None se tiver êxito.
Versão: Python 3.14; repository image, production runtime not attested
Sintaxe
delattr(object, name, /)Remove o atributo indicado segundo as regras de __delattr__ e devolve None se tiver êxito.
Aqui a classe não tem size, pelo que após a remoção hasattr dá False e uma segunda tentativa gera AttributeError.
Um size da classe passaria a ficar visível.
class Box:
size = 3
b = Box()
b.size = 7
assert delattr(b, "size") is None and b.size == 3
Exemplos
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__)
Resultado
False AttributeError
Um size da classe passaria a ficar visível.