delattr
delattr is a built-in function in Python that deletes an attribute from an object by name. It is effectively the explicit counterpart to using the del statement with an attribute, such as del obj.name. The function calls the object's __delattr__(name) method to perform the deletion, which means the exact behavior can depend on the object's class implementation.
- object is the target from which the attribute should be removed.
- name must be a string specifying the attribute to delete.
Behavior: The default deletion behavior attempts to remove the named attribute from the object’s namespace (typically
- Deleting an instance attribute that was previously set on the object, e.g., c.x = 1; delattr(c, 'x').
- Deleting a class attribute by applying delattr to the class itself, e.g., delattr(C, 'attr'), which removes
- Some attributes may be protected by descriptors or custom __delattr__ implementations, making deletion impossible and resulting
- delattr is part of Python’s attribute management family, related to getattr, setattr, and hasattr, and can