Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!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.014 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'exists.': 0.07; 'problem?': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; 'wrote': 0.14; 'defaultdict': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'skip:n 70': 0.16; 'subject:dictionaries': 0.16; "value',": 0.16; "{'a':": 0.16; 'wrote:': 0.18; 'seems': 0.21; '>>>': 0.22; 'appears': 0.22; 'header:X-Complaints-To:1': 0.27; 'getting': 0.31; "skip:' 10": 0.31; 'josh': 0.31; 'subject:with': 0.35; 'problem.': 0.35; 'but': 0.35; 'there': 0.35; 'list': 0.37; 'depends': 0.38; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'even': 0.60; 'skip:u 10': 0.60; 'solve': 0.60; 'simple': 0.61; "you're": 0.61; 'situation': 0.65; 'side': 0.67; 'frank': 0.68; 'default': 0.69; '2:02': 0.84; 'abc': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: "Frank Millman" Subject: Re: Keeping track of things with dictionaries Date: Tue, 8 Apr 2014 09:14:39 +0200 References: <534105ce$0$1365$4fafbaef@reader1.news.tin.it> <21ef5159-ad95-4d43-a2d6-7ecda941d978@googlegroups.com> X-Gmane-NNTP-Posting-Host: 197.87.50.147 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.3790.4657 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.4913 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: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1396941292 news.xs4all.nl 2883 [2001:888:2000:d::a6]:34709 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:69836 "Chris Angelico" wrote in message news:CAPTjJmqFBt2XX+BDfNHz0gaGOrDkhtpBzrR29DUWN36girzcSw@mail.gmail.com... > On Tue, Apr 8, 2014 at 2:02 PM, Josh English > wrote: >> >> Would dict.setdefault() solve this problem? Is there any advantage to >> defaultdict over setdefault() > > That depends on whether calling Brand() unnecessarily is a problem. > Using setdefault() is handy when you're working with a simple list or > something, but if calling Brand() is costly, or (worse) if it has side > effects that you don't want, then you need to use a defaultdict. > It appears that when you use 'setdefault', the default is always evaluated, even if the key exists. >>> def get_value(val): ... print('getting value', val) ... return val*2 ... >>> my_dict = {} >>> my_dict.setdefault('a', get_value('xyz')) getting value xyz 'xyzxyz' >>> my_dict.setdefault('a', get_value('abc')) getting value abc 'xyzxyz' >>> my_dict {'a': 'xyzxyz'} >>> It seems odd. Is there a situation where this behaviour is useful? Frank Millman