Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'append': 0.07; 'important,': 0.07; 'override': 0.07; 'received:mail- vc0-f174.google.com': 0.09; 'sure,': 0.09; 'yeah,': 0.09; 'subject:not': 0.11; 'sat,': 0.15; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'setdefault': 0.16; 'subject:already': 0.16; 'subject:key': 0.16; 'thread-safe': 0.16; 'threads': 0.16; 'wrote:': 0.17; 'jan': 0.18; '>>>': 0.18; 'trying': 0.21; 'header:In-Reply-To:1': 0.25; 'message- id:@mail.gmail.com': 0.27; 'fixed': 0.28; 'dictionary': 0.29; 'received:209.85.220.174': 0.29; "skip:' 10": 0.30; 'problem': 0.33; 'to:addr:python-list': 0.33; 'skip:d 20': 0.34; 'received:google.com': 0.34; 'list': 0.35; 'doing': 0.35; 'pm,': 0.35; 'received:209.85.220': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'why': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'skip:l 20': 0.38; 'to:addr:python.org': 0.39; 'different': 0.63; 'times': 0.63; 'guaranteed': 0.76; '2013': 0.84; 'lists:': 0.91; 'subject:add': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=QrPFNkZ913/nWRYcq4kMkIOrMd/ghvdl9w7iA9XjL9Q=; b=UBBgElBnZJlD5PHMSuwn+alA9bs4GQZoa8O7dl4gPrLGYmYVbjyIhSwKaPp+bCRMaV SXU4+5F24ivGSuAaH7V58TdjzuZMvdHZzKBiNe4Y2IHSso2mxbs/b7aovAiynPuYnBPg zVRJJS68gosyTYfBkv2MoNl4t/Eom6FS7EXEY8ZM3fy9x8WbBefGdu8fKfw0RDSQw51L DI0t61zN0mkH92QEj5l/s5K7GWJL4Cg2PqHSVT6hu8rMrvUeVsecq18pJlRRjqrtItaQ UkkNtVTNsliaNHnzJi1rWNj5uubkMRI1hPd6RuIvCu/Xg9HgHfWRDVhgPh4dNSxqjZpY KswA== MIME-Version: 1.0 X-Received: by 10.221.11.205 with SMTP id pf13mr12434615vcb.70.1358583831215; Sat, 19 Jan 2013 00:23:51 -0800 (PST) In-Reply-To: References: <50fa1bf1$0$30003$c3e8da3$5496439d@news.astraweb.com> Date: Sat, 19 Jan 2013 19:23:51 +1100 Subject: Re: Safely add a key to a dict only if it does not already exist? From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-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: 31 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1358583833 news.xs4all.nl 6869 [2001:888:2000:d::a6]:38154 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:37071 On Sat, Jan 19, 2013 at 7:16 PM, Vito De Tullio wrote: > yeah, sure, but with a fixed value :) > > I mean: if the value is not important, why bother at all trying not to > override it with an if or a lock or other tecniques? doing > > d['key'] = 'fixed_value' > > multiple times in different threads is not a problem in my eyes... How fixed is fixed? >>> d={} >>> d.setdefault("foo",1) 1 >>> d.setdefault("bar",2) 2 >>> d {'bar': 2, 'foo': 1} If list append is guaranteed atomic, and since setdefault is apparently atomic, then this would be a thread-safe way to maintain a dictionary of lists: >>> d={} >>> lst=d.setdefault("foo",[]) >>> lst.append(1) Are those guarantees made? ChrisA