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


Groups > comp.lang.python > #81767

Re: Setting default_factory of defaultdict to key

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ethan@stoneleaf.us>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'modify': 0.07; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'undefined': 0.09; '~ethan~': 0.09; 'def': 0.12; 'missed': 0.12; '10:05': 0.16; 'accesses': 0.16; 'defaultdict': 0.16; 'filename:fname piece:signature': 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; 'to:name:python- list@python.org': 0.22; 'header:User-Agent:1': 0.23; 'mon,': 0.24; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'dec': 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; '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; 'received:173': 0.61; 'here': 0.66; 'ethan': 0.84; 'furman': 0.84; 'spoke': 0.91; 'subject:Setting': 0.96
Date Mon, 01 Dec 2014 10:36:25 -0800
From Ethan Furman <ethan@stoneleaf.us>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0
MIME-Version 1.0
To "python-list@python.org" <python-list@python.org>
Subject Re: Setting default_factory of defaultdict to key
References <CACwCsY7sCi51Aq9z3vbSohG-gq-xHyo_=jXzS04FUVy1b8F5Qw@mail.gmail.com> <547CB12C.3010502@stoneleaf.us> <CACwCsY5oCdq5b0ZO3iXV21CutV91tF8=wwu+gpYDbC4X0=p0wQ@mail.gmail.com>
In-Reply-To <CACwCsY5oCdq5b0ZO3iXV21CutV91tF8=wwu+gpYDbC4X0=p0wQ@mail.gmail.com>
Content-Type multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="7xASEtkCp9RIo3vw3dTvpuFn4ul8M0r79"
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.16463.1417458990.18130.python-list@python.org> (permalink)
Lines 76
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1417458990 news.xs4all.nl 2859 [2001:888:2000:d::a6]:59110
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:81767

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

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]

--
~Ethan~

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


Thread

Re: Setting default_factory of defaultdict to key Ethan Furman <ethan@stoneleaf.us> - 2014-12-01 10:36 -0800

csiph-web