Sorry, I couldn't think of a better title, naming my use-example of a fuzzy dict wouldn't explain anything.
I want to write a fuzzy dict. The keys would be tuples of integers but retrieval would retrieve the value for that key and for neighbouring keys too so
fd = FuzzyDict()
fd[(0,0)] = {x1,x2,x3}
fd[(0,1)] = (x4,x5}
fd[(1,0)] = {x6,x7,x8}
print(f[(0,0)])
# gives {x1,x2,x3,x4,x5,x6,x7,x8}
I can do this, it's just a matter of writing __setitem__()
and __getitem__()
for my class something like this
def __getitem__(self,key):
answer = {}
for nbr in neighours
answer |= self.innerdict[key+nbr]
return answer
The fun starts happening when I write
fd[(0,1)] |= {x10}
# or, equivalently
fd[(0,1)] = fd[(0,1)] + {x10}
because the fd[(0,1]
of the left of the assignment is by reference by variable and the fd[(0,1)]
on the right of the assignment is by value. My code would look more elegant if I could achieve this separation, otherwise I will have to write a normal __getitem__()
and an extra function for the fuzzy-fetch.
Is there a way of achieving this distinction in python?