#!/usr/bin/env python3 # -*- coding: utf-8 -*- # A mapping class only implementing __getitem__ class WithoutSetItem: def __getitem__(self, key): return "foo" # A mapping class also implementing __setitem__ class WithSetItem: def __getitem__(self, key): return "foo" def __setitem__(self, key, val): return # Trying to use del on an item of the calss without __setitem__ # results in "TypeError: 'WithoutSetItem' object doesn't support item deletion" wo = WithoutSetItem() try: del wo[0] except Exception as e: print("WithoutSetItem =>", repr(e)) # Trying to use del on an item of the calss with __setitem__ # results in "AttributeError: __delitem__" w = WithSetItem() try: del w[0] except Exception as e: print("WithSetItem =>", repr(e))