Re: Advanced Dictionary

2010-06-16 Thread Stephen Hansen
On 6/16/10 9:34 AM, Steven D'Aprano wrote: > On Wed, 16 Jun 2010 09:17:47 -0700, Stephen Hansen wrote: > >> Leading-and-trailing double underscores are explicitly reserved for >> Python to define as Special. > > > That part is correct. But of course Python doesn't prevent you from > ignoring t

Re: Advanced Dictionary

2010-06-16 Thread Steven D'Aprano
On Wed, 16 Jun 2010 09:17:47 -0700, Stephen Hansen wrote: > Leading-and-trailing double underscores are explicitly reserved for > Python to define as Special. That part is correct. But of course Python doesn't prevent you from ignoring this rule (more of a guideline really). > They also imp

Re: Advanced Dictionary

2010-06-16 Thread Stephen Hansen
On 6/16/10 6:10 AM, Peter Otten wrote: > Thomas Lehmann wrote: > >>> class AutoValueDict(dict): >>> def __makeitem__(self, key): >>> return self.setdefault(key, {}) > > I think it's bad style to invent your own __whatever__() methods, I'd rather > call them _whatever(). It goes a bi

Re: Advanced Dictionary

2010-06-16 Thread Ian Kelly
On Wed, Jun 16, 2010 at 4:43 AM, Thomas Lehmann wrote: > Hi, > > I have seen a recipe which allows auto creation of missing values for > dictionaries. > However this recipe is not working for all. > > class AutoValueDict(dict): >    def __makeitem__(self, key): >        return self.setdefault(key,

Re: Advanced Dictionary

2010-06-16 Thread Peter Otten
Thomas Lehmann wrote: >> class AutoValueDict(dict): >> def __makeitem__(self, key): >> return self.setdefault(key, {}) I think it's bad style to invent your own __whatever__() methods, I'd rather call them _whatever(). >> def __getitem__(self, key): >> return self.get(ke

Re: Advanced Dictionary

2010-06-16 Thread Thomas Lehmann
> > class AutoValueDict(dict): >     def __makeitem__(self, key): >         return self.setdefault(key, {}) > >     def __getitem__(self, key): >         return self.get(key, self.__makeitem__(key)) > > I would like to have a dictionary which ensures dictionaries as values > except when I'm assigni