1class TwoWayDict(dict):
2 def __delitem__(self, key):
3 value = super().pop(key)
4 super().pop(value, None)
5 def __setitem__(self, key, value):
6 if key in self:
7 del self[self[key]]
8 if value in self:
9 del self[value]
10 super().__setitem__(key, value)
11 super().__setitem__(value, key)
12 def __repr__(self):
13 return f"{type(self).__name__}({super().__repr__()})"
14