If you want both dicts to remain independent and updated, you can create one object that queries both dictionaries in its __getitem__ method (and implements get , __contains__ and the other matching method as necessary).
An example of minimalism might be this:
class UDict(object): def __init__(self, d1, d2): self.d1, self.d2 = d1, d2 def __getitem__(self, item): if item in self.d1: return self.d1[item] return self.d2[item]
And it works:
>>> a = UDict({1:1}, {2:2}) >>> a[2] 2 >>> a[1] 1 >>> a[3] Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 7, in __getitem__ KeyError: 3 >>>
jsbueno Mar 22 '12 at 11:24 2012-03-22 11:24
source share