Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #22188

Re: Question about collections.defaultdict

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.mixmin.net!feed.xsnews.nl!border-3.ams.xsnews.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.018
X-Spam-Evidence '*H*': 0.96; '*S*': 0.00; 'assuming': 0.09; 'constructor': 0.09; 'method,': 0.09; 'override': 0.09; 'stealing': 0.09; 'declared': 0.16; 'enigma': 0.16; 'kern': 0.16; 'received:80.91': 0.16; 'received:80.91.229': 0.16; 'received:gmane.org': 0.16; 'received:list': 0.16; 'subclass': 0.16; 'subclasses': 0.16; 'underlying': 0.16; 'robert': 0.18; 'previously': 0.20; 'wrote:': 0.21; 'instead.': 0.22; 'subject:Question': 0.22; 'header:In-Reply-To:1': 0.22; 'header :User-Agent:1': 0.23; 'seems': 0.28; 'pm,': 0.28; '(by': 0.29; 'default,': 0.29; 'code': 0.29; 'class': 0.29; "i'd": 0.29; 'url:python': 0.34; 'there': 0.35; '(and': 0.35; 'skip:_ 10': 0.35; 'header:X-Complaints-To:1': 0.36; 'web,': 0.36; 'url:library': 0.36; 'but': 0.36; 'url:org': 0.36; 'thank': 0.37; 'list': 0.37; 'called': 0.38; 'received:org': 0.38; "can't": 0.39; 'to:addr:python-list': 0.39; 'allow': 0.39; 'to:addr:python.org': 0.40; 'act': 0.60; 'your': 0.60; 'relatively': 0.63; 'our': 0.64; 'easy': 0.65; 'world': 0.65; 'lower': 0.67; 'believe': 0.67; 'realized': 0.72; 'terrible': 0.84; 'eco': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Robert Kern <robert.kern@gmail.com>
Subject Re: Question about collections.defaultdict
Date Mon, 26 Mar 2012 14:44:51 +0100
References <4F707028.4000407@syslang.net>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host 94.197.127.115.threembb.co.uk
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:11.0) Gecko/20120313 Thunderbird/11.0
In-Reply-To <4F707028.4000407@syslang.net>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1002.1332769506.3037.python-list@python.org> (permalink)
Lines 38
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1332769506 news.xs4all.nl 6985 [2001:888:2000:d::a6]:56783
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:22188

Show key headers only | View raw


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 comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Question about collections.defaultdict Robert Kern <robert.kern@gmail.com> - 2012-03-26 14:44 +0100

csiph-web