1

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?

martineau
  • 119,623
  • 25
  • 170
  • 301
Haydon Berrow
  • 485
  • 2
  • 6
  • 14
  • It's not really clear to me what exactly you want. How does "by reference by variable" differ from "by value"? What is the separation you want to achieve? – MisterMiyagi Nov 30 '21 at 17:55
  • It's not clear to me what you want the exact behaviour to be. What should happen on item assignment, _exactly_? Python does not have a concept of by-reference versus by-value, it's all by-object-identity, so your clarifying paragraph doesn't help. – Jasmijn Nov 30 '21 at 17:56
  • 1
    Note that Python really has no concept of "value" vs "variable". A "value literal" like ``1`` and a "value variable" like ``a`` (assuming we assigned ``a = 1`` previously) both are just *expressions evaluating to* an object representing the number 1. That "number 1" has no concept of expressing whether it "comes from" a literal, a variable, or any other expression such as a subscription, arithmetic, call, or similar. – MisterMiyagi Nov 30 '21 at 18:02
  • A "fuzzy dict" is not a useful concept. If you want to write a function that does "fuzzy lookup" from a regular dict, THAT is both possible and potentially useful, but by changing the semantics of a dictionary, you are making your code unmaintainable. – Tim Roberts Nov 30 '21 at 18:19

1 Answers1

0

Thank you to everybody who replied. As I thought, there is no distinction between variable and value. MisterMiyagi put it best. I elected for this code

import collections
class BlurredDict(collections.defaultdict):

    def __init__(self,blur=(1,1)):
        self.default_factory = lambda:set()
        self.nbs = []
        for i in range(-blur[0],blur[0]+1):
            for j in range(-blur[1],blur[1]+1):
                self.nbs.append( (i,j) )

    def values(self,key):
        answer = set()
        _add = lambda a,b:tuple([x+y for x,y in zip(a,b)])
        for n in self.nbs:
            answer |= self[_add(key,n)]
        return answer

if __name__ == "__main__":
    bd = BlurredDict()
bd[(3,4)] = {34}
bd[(3,4)] |= {1000}
bd[(2,4)] = {24,0}
bd[(3,3)] = {33}
bd[(2,4)].remove(0)
print(f"{bd.values((3,4)) = }")

My application was in the simulation of physical processes in which the influence fades with distance. This code makes it easier to exhaust over nearby objects.

Haydon Berrow
  • 485
  • 2
  • 6
  • 14