In Python, delattr(obj, "name") removes an attribute and does the same as del obj.name, but with the name given as a string. Afterwards, reading that attribute raises AttributeError, unless a class attribute with the same name shows through.
It is useful for cleanup code where names come from data, for example removing all temporary attributes that start with tmp_. It also works on classes, which removes the attribute for every instance that does not have its own copy.
delattr() is a built-in function: it lives in the builtins module, so it is always available without an import.
Syntax
Pass the object and the attribute name.
python
delattr(obj, "name")
Call
What it does
delattr(p, "nickname")
Same as del p.nickname.
delattr(obj, name)
Name taken from a variable.
delattr(Class, "x")
Removes a class attribute.
delattr(p, "missing")
Raises AttributeError.
Deleting an instance attribute can reveal a class attribute of the same name.
Library declarations
python
delattr(object, name, /)
Parameters
Parameter
Description
object
Required. The object to modify.
name
Required. The attribute name as a string.
Return Value
Return Value
Description
None
Nothing; the attribute is removed.
Python Examples
Example 1 — Delete an Attribute and Try Again
Remove one attribute, check the result, and delete it a second time.
python
class Profile:
def __init__(self):
self.name = "Ana"
self.nickname = "A"
p = Profile()
delattr(p, "nickname")
print(hasattr(p, "nickname"))
print(vars(p))
try:
delattr(p, "nickname")
except AttributeError:
print("nickname is already gone")
Output
False
{'name': 'Ana'}
nickname is already gone
How It Works
Removed. After delattr(), hasattr() reports False.
Gone from __dict__. Only name remains in the instance dictionary.
Second delete fails. There is nothing left to remove, so Python raises AttributeError.
Same as del.del p.nickname would behave identically.
Example 2 — Remove All Temporary Attributes
Find attribute names with a prefix and delete them in a loop.
python
class Cache:
def __init__(self):
self.users = [1, 2]
self.tmp_page = "<html>"
self.tmp_query = "SELECT"
c = Cache()
for name in [n for n in vars(c) if n.startswith("tmp_")]:
delattr(c, name)
print(sorted(vars(c)))
Copy before deleting. The list comprehension builds a separate list, so the dict is not changed while it is being looped over.
Dynamic delete.delattr() takes each name from the loop variable.
What remains. Only users survives the cleanup.
Example 3 — Instance Attributes Shadow Class Attributes
Delete an instance attribute to reveal the class value, then delete the class attribute.
python
class Theme:
color = "blue"
t = Theme()
t.color = "red"
print(t.color)
delattr(t, "color")
print(t.color)
try:
delattr(t, "color")
except AttributeError:
print("the instance has no color of its own now")
delattr(Theme, "color")
print(hasattr(t, "color"))
Output
red
blue
the instance has no color of its own now
False
How It Works
Shadowing. Setting t.color hides the class value "blue".
Revealed. Deleting the instance attribute makes t.color fall back to the class.
Not the class attribute. A second delattr(t, ...) fails: the instance has nothing left to delete.
Delete on the class.delattr(Theme, "color") removes it for every instance.
Frequently Asked Questions
It deletes an attribute by name: delattr(obj, "x") is the same as del obj.x.
When the attribute name is in a variable or comes from data.
The object has no such attribute of its own. Class attributes must be deleted from the class.
Yes, from the class: delattr(MyClass, "method"). Instances normally do not own their methods.