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 assigning
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, {})
def __getitem__(self, key):
return self.get(key,
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(key,
On Wed, Jun 16, 2010 at 4:43 AM, Thomas Lehmann t.lehm...@rtsgroup.net 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
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 bit beyond bad
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 imply
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 this rule