Check for dict key existence, and modify it in one step.

Dustan DustanGroups at gmail.com
Tue Aug 28 21:09:46 EDT 2007


On Aug 28, 1:13 pm, rodrigo <rodrigo... at gmail.com> wrote:
> evan,
>
> yes, it does help. Works like it should:
>
> class CountingDictionary(dict):
>     def increment(self, key, delta=1):
>         self[key] = self.get(key, 0) + delta
>
> d = CountingDictionary()
> d.increment('cat')
> d.increment('dog',72)
> print d
>
> >>> {'dog': 72, 'cat': 1}
>
> Thanks!

You responded to the answer that made the least use of already
existing recourses in python. Just letting you know.




More information about the Python-list mailing list