magical expanding hash

Steven Bethard steven.bethard at
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

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)


More information about the Python-list mailing list