Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin3!goblin.stu.neva.ru!newsfeed3.funet.fi!newsfeeds.funet.fi!uio.no!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.047 X-Spam-Evidence: '*H*': 0.91; '*S*': 0.00; 'yet.': 0.04; 'def': 0.12; 'bug': 0.12; 'defaultdict': 0.16; 'dict': 0.16; 'key):': 0.16; 'self[key]': 0.16; 'subclass': 0.16; 'subject:key': 0.16; 'thread,': 0.16; 'fix': 0.17; 'wrote:': 0.18; "hasn't": 0.19; 'header:User-Agent:1': 0.23; 'earlier': 0.24; 'looks': 0.24; 'equivalent': 0.26; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'filed': 0.31; 'class': 0.32; 'beginning': 0.33; 'skip:_ 10': 0.34; 'point.': 0.35; 'but': 0.35; 'subject:skip:d 10': 0.36; 'method': 0.36; 'whatever': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'ian': 0.60; 'ago,': 0.61; 'john': 0.61; 'provide': 0.64; 'received:74.208': 0.68; 'said:': 0.68; 'confusing': 0.84; 'received:74.208.4.194': 0.84; 'surface': 0.84; 'subject:Setting': 0.96 Date: Sun, 07 Dec 2014 11:31:07 -0500 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Setting default_factory of defaultdict to key References: <54821AEB.1050207@verizon.net> In-Reply-To: <54821AEB.1050207@verizon.net> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:W2gy4FJs3UVR09c92agEA3kKCcBpuPWZl5qYhgYIuU+ cWSdiwbhRCxb2fnGvu1mIpiF9s7pGfb7IMVLw11MKNT9A/vUDG 1g8TPxNwyIH1PFK+WiOJDvw21hPQEFAp6/D4gu8ZrKAs5nyHO+ wlNeSq4xRoBmfMIAqXBwUbsAqtuBde07xUteo7DgRVXzxZnavD ouQhW7rv74azlBHPn1pFWUGLcUfNt6aYxIOh7j7i2p4oUsK9HG pM2wm0GPwHgwoRWRhNUQRm+hCkROUKQpWGV63ZxsJGOeLSYib7 kkHI8QC/j6u0kSPYlY81260KM4b+u0A2hWffdPhHuH35UtSNLf ip2da3Sl0yzeQN8zzpCY= X-UI-Out-Filterresults: notjunk:1; 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: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1417969885 news.xs4all.nl 2908 [2001:888:2000:d::a6]:47300 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:82066 On 12/05/2014 03:51 PM, John J Posner wrote: > At the beginning of this thread, Ian Kelly said: Since this clearly is intended to be part of the earlier thread, please make it so by using reply-list or whatever equivalent your email program has. > > Not with defaultdict, but you can subclass dict and provide a > __missing__ method directly > > To emphasize, you don't need to subclass "defaultdict" -- you need only > subclass "dict" itself: > > class MyDict(dict): > def __missing__(self, key): > self[key] = key > return key > > md = MyDict() > md[1] = 111 > _ = md[2] > _ = md["another key"] > > > ## md now looks like this: {1: 111, 2: 2, 'another key': 'another key'} > > The defaultdict documentation is confusing on this point. A *long* time > ago, I filed Bug 9536 to improve the doc, but the fix hasn't bubbled to > the surface yet. > > -- DaveA