Jeff Epler wrote: > The software you used to post this message wrapped some of the lines of > code. For example: >> def __delitem__(self, key): >> super(keytransformdict, self).__delitem__(self, >> self._transformer(key))
Somehow I feared that this would happen. > In defaultdict, I wonder whether everything should be viewed as a > factory: > def setdefaultvalue(self, value): > def factory(): return value > self.setdefaultfactory(factory) That's a reasonable approach. __init__ must be changed too, but this shouldn't hurt too badly. > and the "no-default" mode would either cease to exist, or > def cleardefault(self): > def factory(): > raise KeyError, "key does not exist and no default defined" > self.setdefaultfactory(factory) > (too bad that the key isn't available in the factory, this degrades the > quality of the error messge) That can be worked around with a solution in __getitem__, see below. > if so, __getitem__ becomes simpler: > __slots__ = ['_default'] > def __getitem__(self, key): > try: > return super(defaultdict, self).__getitem__(key) > except KeyError: > return self.setdefault(key, apply(*self._default)) You are peculating the kwargs. Also, apply() is on the verge of being deprecated, so better not use it. def __getitem__(self, key): try: return super(defaultdict, self).__getitem__(key) except KeyError, err: try: return self.setdefault(key, self._default[0](*self._default[1], **self._default[2])) except KeyError: raise err Although I'm not sure whether KeyError would be the right one to raise (perhaps a custom error?). > I don't ever have an itch for sorted dictionaries, as far as I can > remember, and I don't immediately understand the use of > keytransformdict. Can you give an example of it? See the thread "Case-insensitive dict, non-destructive, fast, anyone?", starting at 04/01/05 12:38. mfg Georg -- http://mail.python.org/mailman/listinfo/python-list