magical expanding hash

Steven Bethard steven.bethard at gmail.com
Tue Jan 17 13:05:01 EST 2006


Paul Rubin wrote:
> Hmm,
> 
>    x[a][b][c][d] = e    # x is a "magic" dict
> 
> becomes
> 
>    x.setdefault(a,{}).setdefault(b,{}).setdefault(c,{})[d] = e
> 
> if I understand correctly.  Ugh.

Agreed.  I really hope that Python 3.0 applies Raymond Hettinger's 
suggestion "Improved default value logic for Dictionaries" from
     http://wiki.python.org/moin/Python3%2e0Suggestions

This would allow you to make the setdefault() call only once, instead of 
on every lookup:

     class meh(dict):
         def __init__(self, *args, **kwargs):
             super(meh, self).__init__(*args, **kwargs)
             self.setdefault(function=meh)

STeVe




More information about the Python-list mailing list