__delitem__

Dunder MethodPython 2.0+Advanced

Enables item deletion with del obj[key]

Quick Info

Documentation
Official Docs
Python Version
2.0+

Learn by Difficulty

Quick Example

python
class TrackedDict:
    def __init__(self):
        self._data = {"a": 1, "b": 2, "c": 3}

    def __delitem__(self, key):
        print(f"Deleting key: {key}")
        del self._data[key]

d = TrackedDict()
del d["b"]
print(d._data)

__delitem__ enables item deletion with del obj[key]. Implementing it lets you customize how Python interacts with your objects.

Try in Playground

Tags

oopmagic-methodprotocolcore