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


Groups > comp.lang.python > #81764

Re: Setting default_factory of defaultdict to key

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <larry.martell@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.009
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'modify': 0.07; 'undefined': 0.09; 'def': 0.12; '10:05': 0.16; 'accesses': 0.16; 'defaultdict': 0.16; 'key):': 0.16; 'key?': 0.16; 'perfect.': 0.16; 'self[key]': 0.16; 'subclass': 0.16; 'subject:key': 0.16; '{})': 0.16; 'wrote:': 0.18; 'to:name:python-list@python.org': 0.22; 'mon,': 0.24; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'dec': 0.30; 'message-id:@mail.gmail.com': 0.30; 'along': 0.30; 'keys': 0.31; 'larry': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; 'skip:d 20': 0.34; 'received:google.com': 0.35; 'there': 0.35; 'much.': 0.36; 'subject:skip:d 10': 0.36; 'thanks': 0.36; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'ethan': 0.84; 'furman': 0.84; 'subject:Setting': 0.96
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=4CIF7/1QOa4CbZx0th5TlHwXPFMUki6MP6uvK3k2sa0=; b=t5bTaGXCVCfWBvZ87tCaiZ9yuWm/7JQpqYEr3uRKyLAMIsfoEL8gKdl+iK8KnGn4aV lZ8FgAM5Xs0Y8DtCL6fKOslf9PUFJ39a3gvfdvjW1XNDVDBfJ/qF2nn2KGkvw8V8nY52 DvRBHr3SDDW+rGYxFc3P7WzJkDCpKy1cFB6U58JsbdC76oFUJ4htFV9dTHDvKLyx3xqF wI//EtUPHI+vc152spzD8TvAk/3cGK3WxnS3qJtAVT3xgdPwGqD41Oj1so4lXge9ulGw hxgILu0Y3WVCZgxbRDoIH12/lRoL40k87u2GtTnyeZJ5+kvNyHPIdFwwMg2wRKERx9Kv L8Sg==
MIME-Version 1.0
X-Received by 10.194.61.227 with SMTP id t3mr84298434wjr.96.1417458227306; Mon, 01 Dec 2014 10:23:47 -0800 (PST)
In-Reply-To <547CB12C.3010502@stoneleaf.us>
References <CACwCsY7sCi51Aq9z3vbSohG-gq-xHyo_=jXzS04FUVy1b8F5Qw@mail.gmail.com> <547CB12C.3010502@stoneleaf.us>
Date Mon, 1 Dec 2014 13:23:47 -0500
Subject Re: Setting default_factory of defaultdict to key
From Larry Martell <larry.martell@gmail.com>
To "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.16460.1417458228.18130.python-list@python.org> (permalink)
Lines 30
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1417458228 news.xs4all.nl 2925 [2001:888:2000:d::a6]:46781
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:81764

Show key headers only | View raw


On Mon, Dec 1, 2014 at 1:19 PM, Ethan Furman <ethan@stoneleaf.us> wrote:
> On 12/01/2014 10:05 AM, Larry Martell wrote:
>>
>> Is there a way to set the default_factory of defaultdict so that
>> accesses to undefined keys get to set to the key?
>
> You need to subclass and modify __missing__ to actually pass along the key:
>
> --> class defaultdictkey(defaultdict):
> ...   def __missing__(self, key):
> ...     self[key] = self.default_factory(key)
> ...     return self[key]
> ...
>
> and in action:
>
> --> huh = defaultdictkey(lambda k: k)
> --> huh
> defaultdict(<function <lambda> at 0x7fe1305de3f0>, {})
> --> huh['x']
> 'x'
> --> huh['x']
> 'x'
> --> huh.get('y')
> --> huh['y']
> 'y'
> --> huh.get('y')
> 'y'

Perfect. Thanks much.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Setting default_factory of defaultdict to key Larry Martell <larry.martell@gmail.com> - 2014-12-01 13:23 -0500

csiph-web