Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #81765
| Path | csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1.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.007 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'modify': 0.07; 'undefined': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; '10:05': 0.16; 'accesses': 0.16; 'defaultdict': 0.16; 'key):': 0.16; 'key?': 0.16; 'self[key]': 0.16; 'subclass': 0.16; 'subject:key': 0.16; '{})': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'mon,': 0.24; 'cc:2**0': 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; 'subject:skip:d 10': 0.36; 'too': 0.37; 'pm,': 0.38; 'ethan': 0.84; 'furman': 0.84; 'spoke': 0.91; '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 :cc:content-type; bh=GfMlElyFUcNriUpeG32OXJG/ChC/Ttv8aNdOgzdoQhQ=; b=Pxvj4NHA2bXTX+4f62bSBCpukzWlrdgJyj+xwdHYmAcxStL7KYpCewaUobIHrsPWlg iCnHLnWbof3HxZCE+J2FNUfTfgenfU9TJrYF1+eJnXeR1DRiCXxlcUn8FP33bbA4ZFgD QMwalE6W2bkBI2iaQopxZqs/aGkHxyK0mj3zFavYXVroD5OSoOVn8rEstrIIwEjfFTja RnfC/crY5v7FHLvIrcLN/a9uL+29R0CPTXSiLDpx+g1dGLxxR9m5aE6PARACgd805aEz jQZ9qhM9tRHrGqmpIRKlMPo9JYeItseBzfYSZmXXWPQ53osy+51L0pqQprl15kUIKw+9 1Bsw== |
| MIME-Version | 1.0 |
| X-Received | by 10.181.8.98 with SMTP id dj2mr72781354wid.81.1417458573350; Mon, 01 Dec 2014 10:29:33 -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:29:33 -0500 |
| Subject | Re: Setting default_factory of defaultdict to key |
| From | Larry Martell <larry.martell@gmail.com> |
| To | Ethan Furman <ethan@stoneleaf.us> |
| Content-Type | text/plain; charset=UTF-8 |
| Cc | "python-list@python.org" <python-list@python.org> |
| 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.16461.1417458574.18130.python-list@python.org> (permalink) |
| Lines | 44 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1417458574 news.xs4all.nl 2941 [2001:888:2000:d::a6]:52587 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:81765 |
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'
I spoke too soon:
>>> class defaultdictkey(defaultdict):
... def __missing__(self, key):
... self[key] = self.default_factory(key)
...
>>> x = defaultdictkey(lambda k: k)
>>> print x['aaa']
None
>>> print x['aaa']
aaa
>>> a = x['qqq']
>>> print a
None
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Setting default_factory of defaultdict to key Larry Martell <larry.martell@gmail.com> - 2014-12-01 13:29 -0500
csiph-web