__delattr__Easy Examples

Called when deleting an attribute with del

Implementing __delattr__

Basic implementation of __delattr__ in a class.

python
class Example:
    def __delattr__(self):
        return "Example __delattr__"

obj = Example()
print(obj)

__delattr__ called when deleting an attribute with del. Implementing it lets you customize how Python interacts with your objects.

__delattr__ in action

Seeing __delattr__ called by Python's built-in operations.

python
# How Python calls __delattr__ automatically
class Demo:
    def __init__(self, value):
        self.value = value

    def __delattr__(self):
        print(f"__delattr__ was called!")
        return self

d = Demo(42)
# This triggers __delattr__:
print(d)

Python automatically calls __delattr__ when you use the corresponding operator or function on your object.

Want to try these examples interactively?

Open Easy Playground