Greg Ewing wrote:
Michele Simionato wrote:

def defaultdict(defaultfactory, dictclass=dict):
    class defdict(dictclass):
        def __getitem__(self, key):
            try:
                return super(defdict, self).__getitem__(key)
            except KeyError:
                return self.setdefault(key, defaultfactory())
    return defdict


That looks really nice!

I'd prefer a more elegant name than 'defaultdict', though.
How about 'table'?

By obvious analogy with Icon (where the dictionary-like object was created with the option of a default value) this gets my +1.

regards
 Steve
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005                      http://www.pycon.org/
Steve Holden                           http://www.holdenweb.com/
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to