Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #37071

Re: Safely add a key to a dict only if it does not already exist?

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 <rosuav@gmail.com>
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 <kddkpe$d2n$1@ger.gmane.org>
References <50fa1bf1$0$30003$c3e8da3$5496439d@news.astraweb.com> <CAMZYqRT9SkzNh2BDC1cccCGsbjHw_jWJ-m+DTuc8NwtfAsmRpg@mail.gmail.com> <kddhse$og0$2@ger.gmane.org> <kddjv3$787$1@ger.gmane.org> <kddkpe$d2n$1@ger.gmane.org>
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 <rosuav@gmail.com>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.680.1358583833.2939.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Sat, Jan 19, 2013 at 7:16 PM, Vito De Tullio <vito.detullio@gmail.com> 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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Safely add a key to a dict only if it does not already exist? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-19 04:07 +0000
  Re: Safely add a key to a dict only if it does not already exist? Chris Rebert <clp2@rebertia.com> - 2013-01-18 20:15 -0800
    Re: Safely add a key to a dict only if it does not already exist? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-19 10:31 +0000
  Re: Safely add a key to a dict only if it does not already exist? Lie Ryan <lie.1296@gmail.com> - 2013-01-19 16:00 +1100
  Re: Safely add a key to a dict only if it does not already exist? Vito De Tullio <vito.detullio@gmail.com> - 2013-01-19 08:25 +0100
  Re: Safely add a key to a dict only if it does not already exist? Vito De Tullio <vito.detullio@gmail.com> - 2013-01-19 08:27 +0100
  Re: Safely add a key to a dict only if it does not already exist? Mitya Sirenef <msirenef@lightbird.net> - 2013-01-19 02:35 -0500
  Re: Safely add a key to a dict only if it does not already exist? Peter Otten <__peter__@web.de> - 2013-01-19 09:02 +0100
  Re: Safely add a key to a dict only if it does not already exist? Vito De Tullio <vito.detullio@gmail.com> - 2013-01-19 09:16 +0100
  Re: Safely add a key to a dict only if it does not already exist? Mitya Sirenef <msirenef@lightbird.net> - 2013-01-19 03:18 -0500
  Re: Safely add a key to a dict only if it does not already exist? Vito De Tullio <vito.detullio@gmail.com> - 2013-01-19 09:19 +0100
  Re: Safely add a key to a dict only if it does not already exist? Chris Angelico <rosuav@gmail.com> - 2013-01-19 19:23 +1100
  Re: Safely add a key to a dict only if it does not already exist? Roy Smith <roy@panix.com> - 2013-01-19 08:04 -0500

csiph-web