delattr() Delete attribute
Last updated: 2026-09-22
delattr() Delete attribute
delattr(obj, name) deletes attribute name from obj; equivalent to del obj.name.
| Category | Built-in Functions |
|---|---|
| Kind | Built-in Function |
| Python Version | all |
📝 Syntax
delattr(object, name)
⚙️ Parameters
| object Required | The object to delete the attribute from. |
|---|---|
| name Required | The attribute name as a string. |
Returns:None.
💥 Raises
AttributeError— Raised when the attribute does not exist or cannot be deleted.TypeError— Raised when name is not a string.
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
False
🏷️ Related Built-in Functions
💡 Related Cases
More case studies coming soon — check back later.
❓ FAQ
QWhat is the difference between delattr(obj, 'x') and del obj.x?
AThey are fully equivalent. The advantage of delattr is that the attribute name can be a variable or a dynamic string, which suits removing an attribute by name.
QWhat happens when I delete a non-existent attribute?
AIt raises an AttributeError, the same as del obj.nonexist; you can check with hasattr() before deleting.
QCan delattr delete a method?
AYes. A method is also a class attribute; after delattr(instance_or_class, 'method'), calling that method on an instance raises an AttributeError.
QWhat type must the name argument be?
AIt must be a string; passing any other type (such as a number) raises a TypeError.