I am using a re.sub callback to replace substrings with random values, but I would like the random values to be the same across different strings. Since the re.sub callback does
You could use a function object.
class A(object): def __init__(self, mappings): self.mappings = mappings def __call__(self, match): return str(eval(match.group(0)[2:-1], self.mappings)) evaluate = A({'A': 1, 'B': 2})