Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #81768
| Path | csiph.com!usenet.pasdenom.info!gegeweb.org!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!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.011 |
| X-Spam-Evidence | '*H*': 0.98; '*S*': 0.00; 'modify': 0.07; 'undefined': 0.09; 'def': 0.12; 'missed': 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; 'thanks.': 0.20; '>>>': 0.22; '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; '>>>>': 0.31; 'keys': 0.31; 'larry': 0.31; 'class': 0.32; 'third': 0.33; '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; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'here': 0.66; '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 :content-type; bh=DIIBKQFg5s3+i7m2E0Q/QE4s9rRY0c33+xXvegXdK1E=; b=GIuIj+a1yx/oDG2bRuqKAgSxE1NKupz5k9Xb8nEXgI6DPO1tokdG1SUoTcM5znyICu KJMMxnFjOUQuFtRhQ1VgHIn5L0la6QgyRDk0KIadoHPW2Yt3IQVb1jQuL+qeumQuzVMy eLezBjLPHBZHugt9AgwX6tip/k/Oe1RhMp7GRGkSOkEZpTOQ2Z56n/HWkB1N/MrbRrsR Ex0QTZZGp2MMtc5zBcfSm3EYqVL5KBcyHEJI5Gbi94ym0n9LsyuPd25pgbCRhYVVvHTa d367szGo4cSkpZU52FGVfdU+fEdjpdnGyY4Wc3cfN0VkBVKjWv2sIJ1NcwR9gxSLSuBt wFdA== |
| MIME-Version | 1.0 |
| X-Received | by 10.194.61.227 with SMTP id t3mr84420012wjr.96.1417459288208; Mon, 01 Dec 2014 10:41:28 -0800 (PST) |
| In-Reply-To | <547CB529.9080202@stoneleaf.us> |
| References | <CACwCsY7sCi51Aq9z3vbSohG-gq-xHyo_=jXzS04FUVy1b8F5Qw@mail.gmail.com> <547CB12C.3010502@stoneleaf.us> <CACwCsY5oCdq5b0ZO3iXV21CutV91tF8=wwu+gpYDbC4X0=p0wQ@mail.gmail.com> <547CB529.9080202@stoneleaf.us> |
| Date | Mon, 1 Dec 2014 13:41:28 -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.16464.1417459295.18130.python-list@python.org> (permalink) |
| Lines | 43 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1417459295 news.xs4all.nl 2879 [2001:888:2000:d::a6]:33706 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:81768 |
Show key headers only | View raw
On Mon, Dec 1, 2014 at 1:36 PM, Ethan Furman <ethan@stoneleaf.us> wrote:
> On 12/01/2014 10:29 AM, Larry Martell wrote:
>> 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)
>
> You missed the third line here ^
>
> ... return self[key]
Oops! Thanks. Working now.
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:41 -0500
csiph-web