I am implementing a something that uses a dictionary to store data. Additionally to the normal data, it also stores some internal data, all prefixed with _
. However, I want to isolate the user of the library from this data since he is normally not concerned with it. Additionally, I need to set a modified
flag in my class to track if the data was modified.
For all interface functions this worked nicely, here are two examples, one with and one without modification. Note that in this case, I do not hide internal data, because it is intentionally demanded as a key:
def __getitem__(self, key):
return self._data[key]
def __setitem__(self, key, value):
self.modified = True
self._data[key] = value
On some functions, e.g. __iter__
, I filter out everything that starts with _
before I yield the data.
But a single function makes real problems here: popitem
. In its normal behaviour it would just withdraw an arbitrary item and return it while deleting it from the dict. However, here comes the problem: Without deep internal knowledge, I don't know which item will be returned beforehand. But I know that popitem
follows the same rules as items
and keys
. So I did come up with an implementation:
keys = self._data.keys()
for k in keys:
if k.startswith("_"):
continue
v = self._data.pop(k)
self.modified = True
return k, v
else:
raise KeyError('popitem(): dictionary is empty')
This implementation works. But it feels to unpythonic and not at all dynamic or clean. It did also struggle with the idea to raise the exception like this: {}.popitem()
which looks totally insane but would give me at least a dynamic way (e.g. if the exception message or type ever changes, I don't have to adjust).
What I am now after is a cleaner and less crazy way to solve this problem. There would be a way of removing the internal data from the dict, but I'd only take this road as a last resort. So do you have any recipes or ideas for this?