You can define your dictionary using python ABC , which provides an infrastructure for defining abstract base classes . And then overload the pop attribute of python dictionary objects based on your need:
from collections import Mapping class MyDict(Mapping): def __init__(self, *args, **kwargs): self.update(dict(*args, **kwargs)) def __setitem__(self, key, item): self.__dict__[key] = item def __getitem__(self, key): return self.__dict__[key] def __delitem__(self, key): del self.__dict__[key] def pop(self, k, d=None): return k,self.__dict__.pop(k, d) def update(self, *args, **kwargs): return self.__dict__.update(*args, **kwargs) def __iter__(self): return iter(self.__dict__) def __len__(self): return len(self.__dict__) def __repr__(self): return repr(self.__dict__)
Demo:
d=MyDict() d['a']=1 d['b']=5 d['c']=8 print d {'a': 1, 'c': 8, 'b': 5} print d.pop(min(d, key=d.get)) ('a', 1) print d {'c': 8, 'b': 5}
Note. Since @chepner is suggested as the best choice in a comment, you can override a popitem that already returns a key / value pair.
Kasramvd
source share