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: 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: References: <547CB12C.3010502@stoneleaf.us> From: Ian Kelly Date: Mon, 1 Dec 2014 11:35:22 -0700 Subject: Re: Setting default_factory of defaultdict to key To: Python 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Mon, Dec 1, 2014 at 11:29 AM, Larry Martell 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__.