Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1a.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.034 X-Spam-Evidence: '*H*': 0.93; '*S*': 0.00; 'default.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'statements': 0.09; 'collections': 0.16; 'defaultdict': 0.16; 'dict': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:dictionaries': 0.16; 'wrote:': 0.18; 'trying': 0.19; '>>>': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'header:X-Complaints-To:1': 0.27; "doesn't": 0.30; "i'm": 0.30; 'posting': 0.31; '"",': 0.31; 'keyerror:': 0.31; 'file': 0.32; 'probably': 0.32; '(most': 0.33; 'guess': 0.33; 'subject:with': 0.35; 'but': 0.35; 'building': 0.35; 'there': 0.35; 'like,': 0.36; 'sometimes': 0.38; 'initially': 0.38; 'to:addr:python-list': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'ian': 0.60; "you're": 0.61; 'act': 0.63; 'believe': 0.68; 'default': 0.69; 'behavior': 0.77; 'd[1]': 0.84; 'd[2]': 0.84; 'dict()': 0.84; 'dict,': 0.84; 'dict.': 0.84; 'dirty': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Keeping track of things with dictionaries Date: Tue, 08 Apr 2014 10:21:58 +0200 Organization: None References: <534105ce$0$1365$4fafbaef@reader1.news.tin.it> <21ef5159-ad95-4d43-a2d6-7ecda941d978@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd9b44.dip0.t-ipconnect.de User-Agent: KNode/4.11.5 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: 1396945337 news.xs4all.nl 2955 [2001:888:2000:d::a6]:57744 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:69844 Ian Kelly wrote: > One thing I will note as a disadvantage of defaultdict is that > sometimes you only want the default value behavior while you're > initially building the dict, and then you just want a normal dict with > KeyErrors from then on. defaultdict doesn't do that; once > constructed, it will always be a defaultdict. This is one of the statements that I won't believe without trying myself. As I'm posting you can probably guess my findings: >>> from collections import defaultdict >>> d = defaultdict(int) >>> d[0] 0 >>> d.default_factory = str >>> d[1] '' >>> d.default_factory = None >>> d[2] Traceback (most recent call last): File "", line 1, in KeyError: 2 >>> d defaultdict(None, {0: 0, 1: ''}) So you can change a defaultdict's default_factory any time you like, and if you set it to None there will be no default. It will still "be" a defaultdict, but it will act like a normal dict. > You can copy the data > into a normal dict using the dict() constructor, but this feels dirty > to me.