defaultdict of arbitrary depth
Carsten Haese
carsten at uniqsys.com
Fri Aug 17 00:19:13 EDT 2007
On Thu, 2007-08-16 at 20:25 -0700, Paul McGuire wrote:
> [...]
> I've hacked out this recursivedefaultdict which is a
> defaultdict(defaultdict(defaultdict(...))), arbitrarily deep depending
> on the keys provided in the reference.
>
> Please comment.
> [...]
>
> class recursivedefaultdict(object):
> def __init__(self):
> self.__dd = defaultdict(recursivedefaultdict)
> def __getattr__(self,attr):
> return self.__dd.__getattribute__(attr)
> def __getitem__(self,*args):
> return self.__dd.__getitem__(*args)
> def __setitem__(self,*args):
> return self.__dd.__setitem__(*args)
This is shorter:
from collections import defaultdict
class recursivedefaultdict(defaultdict):
def __init__(self):
self.default_factory = type(self)
--
Carsten Haese
http://informixdb.sourceforge.net
More information about the Python-list
mailing list