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


Groups > comp.lang.python > #3005

Re: [Feature Request] dict.setdefault()

Date 2011-04-11 18:25 -0500
From Tim Chase <python.list@tim.thechases.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>
Newsgroups comp.lang.python
Message-ID <mailman.231.1302566962.9059.python-list@python.org> (permalink)

Show all headers | 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