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


Groups > comp.lang.python > #3005

Re: [Feature Request] dict.setdefault()

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python.list@tim.thechases.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:: [': 0.03; 'value,': 0.04; 'args': 0.07; 'added,': 0.09; 'imho.': 0.09; 'overwrite': 0.09; 'subject:()': 0.09; 'tuple': 0.09; 'pm,': 0.11; 'def': 0.13; 'am,': 0.14; 'wrote:': 0.14; '(key': 0.16; '**kwargs):': 0.16; '-tkc': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'kwargs': 0.16; 'kwargs,': 0.16; 'message-id:@tim.thechases.com': 0.16; 'mrab': 0.16; 'received:70.251': 0.16; 'received:dsl.rcsntx.swbell.net': 0.16; 'received:rcsntx.swbell.net': 0.16; 'received:swbell.net': 0.16; 'argument': 0.16; 'subject:] ': 0.16; 'subject:skip:d 10': 0.18; "wouldn't": 0.18; 'tue,': 0.20; 'cc:no real name:2**0': 0.20; 'cc:2**0': 0.20; 'header:In-Reply-To:1': 0.22; 'cc:addr:python- list': 0.22; 'optional': 0.23; 'values.': 0.23; 'structure': 0.24; "i'm": 0.26; 'skip:[ 10': 0.26; 'chris': 0.27; 'concern': 0.29; 'comment': 0.30; 'cc:addr:python.org': 0.31; 'initial': 0.32; 'things': 0.33; 'skip:" 10': 0.34; 'received:70': 0.34; 'difference': 0.35; 'header:User-Agent:1': 0.35; 'allow': 0.36; 'should': 0.37; 'apr': 0.38; 'pretty': 0.38; 'returning': 0.39; 'except': 0.39; 'would': 0.40; 'act': 0.61; '2011': 0.62; 'subject:Request': 0.73; '05:44': 0.84; 'self:': 0.84; 'lost,': 0.91; 'readily': 0.93
Date Mon, 11 Apr 2011 18:25:54 -0500
From Tim Chase <python.list@tim.thechases.com>
User-Agent Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.15) Gecko/20110303 Thunderbird/3.1.9
MIME-Version 1.0
To Chris Angelico <rosuav@gmail.com>
Subject Re: [Feature Request] dict.setdefault()
References <48897e75-c02c-43c7-9f46-39da4bcc406d@p16g2000vbi.googlegroups.com> <1302560207.11756.1.camel@localhost.localdomain> <4DA38391.1040507@mrabarnett.plus.com> <BANLkTikhS0C05q+349XePW-bn94C97EUYg@mail.gmail.com>
In-Reply-To <BANLkTikhS0C05q+349XePW-bn94C97EUYg@mail.gmail.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-AntiAbuse This header was added to track abuse, please include it with any abuse report
X-AntiAbuse Primary Hostname - boston.accountservergroup.com
X-AntiAbuse Original Domain - python.org
X-AntiAbuse Originator/Caller UID/GID - [47 12] / [47 12]
X-AntiAbuse Sender Address Domain - tim.thechases.com
X-Source
X-Source-Args
X-Source-Dir
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
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.231.1302566962.9059.python-list@python.org> (permalink)
Lines 42
NNTP-Posting-Host 82.94.164.166
X-Trace 1302566962 news.xs4all.nl 41114 [::ffff:82.94.164.166]:47275
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:3005

Show key headers only | View raw


On 04/11/2011 05:44 PM, Chris Angelico wrote:
> On Tue, Apr 12, 2011 at 8:41 AM, MRAB<python@mrabarnett.plus.com>  wrote:
>> I'm not sure that "setdefault" should take **kw args for this because
>> of its existing argument structure (key + optional value).
>>
>> A new method like "updatedefault" may be better, IMHO. It would act
>> like "update" except that it wouldn't overwrite existing values.
>
> Wouldn't x.updatedefault(y) be pretty much y.update(x) ?

As I understand, the difference would be the following pseudocode:

  def update(self, d):
    for k,v in dict(d).iteritems():
      self[k] = v

  def updatedefault(self, d={}, **kwargs):
    for k,v in chain(
       dict(d).iteritems(),
       kwargs.iteritems()
       ):
      # MRAB's comment about "wouldn't overwrite existing"
      if k not in self:
        self[k] = v

My concern with the initial request is that dict.setdefault() 
already returns the (existent or defaulted) value, so you can do 
things like

   d.setdefault(my_key, []).append(item)

If you allow it to take multiple kwargs, what would the return 
value be (positionality of kwargs is lost, so returning a tuple 
wouldn't be readily possible)?

Finally, if it were added, I'd call it something like merge()

-tkc



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


Thread

[Feature Request] dict.setdefault() rantingrick <rantingrick@gmail.com> - 2011-04-11 14:35 -0700
  Re: [Feature Request] dict.setdefault() Westley Martínez <anikom15@gmail.com> - 2011-04-11 15:16 -0700
  Re: [Feature Request] dict.setdefault() MRAB <python@mrabarnett.plus.com> - 2011-04-11 23:41 +0100
    Re: dict.setdefault() rantingrick <rantingrick@gmail.com> - 2011-04-11 19:58 -0700
      Re: dict.setdefault() Westley Martínez <anikom15@gmail.com> - 2011-04-12 07:02 -0700
  Re: [Feature Request] dict.setdefault() Chris Angelico <rosuav@gmail.com> - 2011-04-12 08:44 +1000
  Re: dict.setdefault() Raymond Hettinger <python@rcn.com> - 2011-04-11 15:49 -0700
  Re: [Feature Request] dict.setdefault() Chris Rebert <clp2@rebertia.com> - 2011-04-11 15:53 -0700
  Re: [Feature Request] dict.setdefault() MRAB <python@mrabarnett.plus.com> - 2011-04-11 23:59 +0100
  Re: [Feature Request] dict.setdefault() Tim Chase <python.list@tim.thechases.com> - 2011-04-11 18:25 -0500
    Re: dict.setdefault() Raymond Hettinger <python@rcn.com> - 2011-04-11 17:17 -0700

csiph-web