which "dictionary with attribute-style access"?

Andreas Balogh baloand at gmail.com
Wed Oct 21 23:40:01 CEST 2009


Gabriel, thanks for your hint. I've managed to create an implementation of an AttrDict 
passing Gabriels tests.

Any more comments about the pythonicness of this implementation?

class AttrDict(dict):
     """A dict whose items can also be accessed as member variables."""
     def __init__(self, *args, **kwargs):
         dict.__init__(self, *args, **kwargs)
         self.__dict__ = self

     def copy(self):
         return AttrDict(self)

     def __repr__(self):
         return 'AttrDict(' + dict.__repr__(self) + ')'

     @classmethod
     def fromkeys(self, seq, value = None):
         return AttrDict(dict.fromkeys(seq, value))

-- 
Andreas Balogh
baloand (at) gmail.com



More information about the Python-list mailing list