"specialdict" module

Jeff Epler jepler at unpythonic.net
Sun Apr 3 13:25:11 EDT 2005


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))

In defaultdict, I wonder whether everything should be viewed as a
factory:
    def setdefaultvalue(self, value):
        def factory(): return value
        self.setdefaultfactory(factory)

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)

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))

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?

Jeff
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20050403/751636f8/attachment.sig>


More information about the Python-list mailing list