As mentioned in the comments of bennofs , you can use intern() so that identical strings are saved only once:
class InternDict(dict): def __setitem__(self, key, value): if isinstance(value, str): super(InternDict, self).__setitem__(key, intern(value)) else: super(InternDict, self).__setitem__(key, value)
Here is an example of an effect that has:
>>> d = {} >>> d["a"] = "This string is presumably too long to be auto-interned." >>> d["b"] = "This string is presumably too long to be auto-interned." >>> d["a"] is d["b"] False >>> di = InternDict() >>> di["a"] = "This string is presumably too long to be auto-interned." >>> di["b"] = "This string is presumably too long to be auto-interned." >>> di["a"] is di["b"] True
source share