Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #22188 > unrolled thread
| Started by | Robert Kern <robert.kern@gmail.com> |
|---|---|
| First post | 2012-03-26 14:44 +0100 |
| Last post | 2012-03-26 14:44 +0100 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Question about collections.defaultdict Robert Kern <robert.kern@gmail.com> - 2012-03-26 14:44 +0100
| From | Robert Kern <robert.kern@gmail.com> |
|---|---|
| Date | 2012-03-26 14:44 +0100 |
| Subject | Re: Question about collections.defaultdict |
| Message-ID | <mailman.1002.1332769506.3037.python-list@python.org> |
On 3/26/12 2:33 PM, Steven W. Orr wrote: > I created a new class called CaseInsensitiveDict (by stealing from code I found > on the web, thank you very much). The new class inherits from dict. It makes it > so that if the key has a 'lower' method, it will always access the key using lower > > I'd like to change the place where I previously declared a dict > > self.lookup = defaultdict(list) > > so that the new code will allow this new dict to be used instead. But then I > realized I may have painted myself into a small corner: > > Is there a way to use defaultdict so that I can override what *kind* of dict it > will use? No. > I would like the value to still be a list be default, but it seems like I can't > tell defaultdict to use *my* new dict. > > Do I give up on defaultdict? Assuming that your CaseInsensitiveDict subclasses from dict or UserDict, it's relatively easy to make a subclass of your CaseInsensitiveDict act like a defaultdict. Just implement the __missing__(key) method appropriately (and modify the constructor to take the callable, of course). http://docs.python.org/library/stdtypes.html#dict http://docs.python.org/library/collections.html#collections.defaultdict.__missing__ -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
Back to top | Article view | comp.lang.python
csiph-web