I am trying to write a wrapper around two dictionaries, so that they seem like one dictionary (for reading only; writing should raise Exceptions).
I am doing this to save memory, since one of the original dictionaries is needed elsewhere. I also think it's faster than merging the dictionaries, if less than half the elements in the combined dictionaries are going to be looked up.
Here's my attempt:
class LogicalMerge:
def __init__(self, d1, d2):
#d1 and d2 are dictionaries
self.d1 = d1
self.d2 = d2
def __getitem__(self, x):
if x in self.d1:
return self.d1[x]
else:
return self.d2[x]
d1 = {1:2, 3:4}
d2 = {5:10}
d = LogicalMerge(d1, d2)
d[1] # == 2
d[5] # == 10
Are there any design, technical, or performance problems with this approach?