__del__ — Easy Examples
Destructor; called when the object is about to be garbage collected
Implementing __del__
Basic implementation of __del__ in a class.
python
class Example: def __del__(self): return "Example __del__" obj = Example() print(obj)
__del__ destructor; called when the object is about to be garbage collected. Implementing it lets you customize how Python interacts with your objects.
__del__ in action
Seeing __del__ called by Python's built-in operations.
python
# How Python calls __del__ automatically class Demo: def __init__(self, value): self.value = value def __del__(self): print(f"__del__ was called!") return self d = Demo(42) # This triggers __del__: print(d)
Python automatically calls __del__ when you use the corresponding operator or function on your object.
Want to try these examples interactively?
Open Easy Playground