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


Groups > comp.lang.python > #81769

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.fsmpi.rwth-aachen.de!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.075
X-Spam-Evidence '*H*': 0.86; '*S*': 0.01; 'def': 0.12; 'key):': 0.16; 'self[key]': 0.16; 'subject:key': 0.16; 'wrote:': 0.18; 'print': 0.22; 'mon,': 0.24; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'dec': 0.30; 'message-id:@mail.gmail.com': 0.30; '>>>>': 0.31; 'larry': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; 'skip:d 20': 0.34; 'received:google.com': 0.35; 'subject:skip:d 10': 0.36; 'too': 0.37; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; '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:from:date:message-id:subject:to :content-type; bh=Yc0ZKj+KSNcP1ie4pvBLscP3MVl0XbWq7BS/0yVz9bI=; b=KN6JK0wlwPnYf7SqspJOJGqw6EqBAzscVaaZHAEurfWW57wXcWRumgkyV04gMA3q+S 2NKupEm3wZqNKmjgaSmeKuksXgFmDzTOFkIOHeN2yLmGcMJE189X+cLfejz0L+29YF57 gWx2MkYdB1re0ltHb0wLdkeM8eDyhSdl56Db8M7kSUayu2oFFde7UXQtihIJ4uPKPDyX tEU3uWqZMXq408IPkJlWG1CcMghqIqZJAOaRj+nHdS8duDNOj+dCHfhfrqxPZ838iFDr RdDhGqVowDLgVq7bgQGiWztcWjATD+oVMceRY9oNu51YUPjd+/SyWeUhcpSpwOauE24L vmpA==
X-Received by 10.70.130.225 with SMTP id oh1mr8146898pdb.147.1417458963164; Mon, 01 Dec 2014 10:36:03 -0800 (PST)
MIME-Version 1.0
In-Reply-To <CACwCsY5oCdq5b0ZO3iXV21CutV91tF8=wwu+gpYDbC4X0=p0wQ@mail.gmail.com>
References <CACwCsY7sCi51Aq9z3vbSohG-gq-xHyo_=jXzS04FUVy1b8F5Qw@mail.gmail.com> <547CB12C.3010502@stoneleaf.us> <CACwCsY5oCdq5b0ZO3iXV21CutV91tF8=wwu+gpYDbC4X0=p0wQ@mail.gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Mon, 1 Dec 2014 11:35:22 -0700
Subject Re: Setting default_factory of defaultdict to key
To Python <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.16465.1417459396.18130.python-list@python.org> (permalink)
Lines 17
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1417459396 news.xs4all.nl 2841 [2001:888:2000:d::a6]:35443
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:81769

Show key headers only | View raw


On Mon, Dec 1, 2014 at 11:29 AM, Larry Martell <larry.martell@gmail.com> wrote:
> 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

You need to also return the value from __missing__.

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


Thread

Re: Setting default_factory of defaultdict to key Ian Kelly <ian.g.kelly@gmail.com> - 2014-12-01 11:35 -0700

csiph-web