Complete guide to Python's delattr function covering object attribute deletion, dynamic attribute handling, and practical examples.
Last modified April 11, 2025
This comprehensive guide explores Python’s delattr function, which dynamically deletes attributes from objects. We’ll cover basic usage, error handling, and practical examples of attribute management.
The delattr function removes a named attribute from an object. It takes two arguments: the object and the attribute name as a string.
Key characteristics: works with any object that allows attribute deletion. Equivalent to del object.attribute but with dynamic attribute name specification.
Here’s simple usage showing how delattr removes attributes from objects compared to the del statement.
basic_delattr.py
class Person: def init(self, name, age): self.name = name self.age = age
p = Person(“Alice”, 30)
del p.age print(hasattr(p, ‘age’)) # False
delattr(p, ’name’) print(hasattr(p, ’name’)) # False
This example shows two ways to delete attributes. The del statement uses direct attribute access while delattr takes the attribute name as a string.
Both methods achieve the same result but delattr is useful when the attribute name is dynamic or comes from user input.
delattr shines when working with dynamic attribute names. This example demonstrates removing attributes based on runtime conditions.
dynamic_delattr.py
class Config: def init(self): self.debug = True self.log_level = “INFO” self.max_connections = 100
config = Config()
for attr in list(vars(config)): if attr.startswith(‘max_’): delattr(config, attr)
print(hasattr(config, ‘max_connections’)) # False
This code dynamically removes attributes matching a pattern. We use vars() to get all attributes and delattr to delete them.
This approach is useful for configuration management or when cleaning up objects based on naming conventions.
delattr raises AttributeError when trying to delete non-existent attributes. This example shows proper error handling.
error_handling.py
class Product: def init(self, name, price): self.name = name self.price = price
p = Product(“Laptop”, 999.99)
try: delattr(p, ‘discount’) except AttributeError as e: print(f"Error: {e}") # discount
if hasattr(p, ‘warranty’): delattr(p, ‘warranty’) else: print(“warranty attribute doesn’t exist”)
The first attempt raises an error because ‘discount’ doesn’t exist. The second approach safely checks before deletion.
Always consider whether to handle missing attributes gracefully or let the exception propagate.
This example shows how delattr interacts with Python properties and the delattr special method.
properties.py
class Account: def init(self, balance): self._balance = balance
@property
def balance(self):
return self._balance
@balance.setter
def balance(self, value):
self._balance = value
@balance.deleter
def balance(self):
print("Balance deletion requested")
del self._balance
def __delattr__(self, name):
print(f"Deleting {name}")
super().__delattr__(name)
acc = Account(1000) delattr(acc, ‘balance’) # Triggers property deleter and delattr
When deleting a property, Python first calls the property’s deleter method. Then it calls delattr if defined.
The output shows both the property deleter message and the delattr message, demonstrating the call sequence.
This example explores delattr behavior with slots and immutable types like tuples.
slots.py
class Point: slots = [‘x’, ‘y’] def init(self, x, y): self.x = x self.y = y
p = Point(3, 4) delattr(p, ‘x’) # Works print(hasattr(p, ‘x’)) # False
try: delattr(p, ‘z’) except AttributeError as e: print(f"Error: {e}") # z
t = (1, 2, 3) try: delattr(t, ‘index’) except AttributeError as e: print(f"Error: {e}") # ’tuple’ object has no attribute ‘index’
slots allows attribute deletion but prevents adding new attributes. Built-in immutable types don’t support attribute deletion at all.
This demonstrates how object mutability and slots affect delattr behavior.
Prefer del for known attributes: Use direct del when attribute names are fixed
Use delattr for dynamic cases: When attribute names come from variables or input
Check before deletion: Use hasattr when unsure about attribute existence
Implement delattr carefully: For custom attribute deletion behavior
Document deletion effects: Especially when using properties or custom deleters
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all Python tutorials.