Re: Bunch 2.0 - a dict with a default

2010-11-19 Thread nn
On Nov 18, 8:45 pm, Phlip phlip2...@gmail.com wrote: Pythonistas: If everyone likes this post, then the code is a snippet for community edification. Otherwise, it's a question: How to do this kind of thing better? I want a dict() variant that passes these test cases:         map = Map()  

Bunch 2.0 - a dict with a default

2010-11-18 Thread Phlip
Pythonistas: If everyone likes this post, then the code is a snippet for community edification. Otherwise, it's a question: How to do this kind of thing better? I want a dict() variant that passes these test cases: map = Map() assert not(map.has_key('_default')) map =

Re: Bunch 2.0 - a dict with a default

2010-11-18 Thread Chris Kaynor
You may want to look at the collections.defaultdict class. It takes in a factory function for default values. You can also implement your class by overriding the __missing__ method of the dict class, rather than overriding the __getitem__. Both were added in Python 2.5 according to the