Base de conocimientosCentro de exámenes
ESEspañol
Elegir idioma13

CKTIS / Python

delattr()

Elimina el atributo nombrado según las reglas de `__delattr__` y devuelve None si tiene éxito.

Versión: Python 3.14; repository image, production runtime not attested

Sintaxis

delattr(object, name, /)

Elimina el atributo nombrado según las reglas de __delattr__ y devuelve None si tiene éxito.

Aquí la clase no tiene size, así que tras borrarlo hasattr da False y un segundo intento provoca AttributeError.

Un size de clase quedaría visible.

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

Ejemplos

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

Un size de clase quedaría visible.

Fuentes