[Python-Dev] Re: A small proposed change to dictionaries' "get"
method
Skip Montanaro
skip@mojam.com (Skip Montanaro)
Thu, 3 Aug 2000 17:21:04 -0500 (CDT)
>> How about making this a method:
>> def inplace(dict, key, default):
>> value = dict.get(key, default)
>> dict[key] = value
>> return value
eh... I don't like these do two things at once kind of methods. I see
nothing wrong with
>>> dict = {}
>>> dict['hello'] = dict.get('hello', [])
>>> dict['hello'].append('world')
>>> print dict
{'hello': ['world']}
or
>>> d = dict['hello'] = dict.get('hello', [])
>>> d.insert(0, 'cruel')
>>> print dict
{'hello': ['cruel', 'world']}
for the obsessively efficiency-minded folks.
Also, we're talking about a method that would generally only be useful when
dictionaries have values which were mutable objects. Irregardless of how
useful instances and lists are, I still find that my predominant day-to-day
use of dictionaries is with strings as keys and values. Perhaps that's just
the nature of my work.
In short, I don't think anything needs to be changed.
-1 (don't like the concept, so I don't care about the implementation)
Skip