Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #3000
| References | <48897e75-c02c-43c7-9f46-39da4bcc406d@p16g2000vbi.googlegroups.com> |
|---|---|
| Date | 2011-04-11 15:53 -0700 |
| Subject | Re: [Feature Request] dict.setdefault() |
| From | Chris Rebert <clp2@rebertia.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.228.1302562428.9059.python-list@python.org> (permalink) |
On Mon, Apr 11, 2011 at 2:35 PM, rantingrick <rantingrick@gmail.com> wrote:
>
> setdefault should take **kw args in the case of needing to set
> multiple defaults at one time. I would even settle for an *arg list if
> i had to.
What would the return value be? dict.setdefault() doesn't currently
just return None you know.
> Anything is better than...
>
> d.setdefault(blah, blah)
> d.setdefault(blah, blah)
> d.setdefault(blah, blah)
> d.setdefault(blah, blah)
> if blah is not blah:
> d.setdefault(blah, blah)
The redundancy is easily removed:
defaults = {blah: blah, blah: blah, blah: blah, blah: blah}
defaults.update(d) # clobber defaults with specified vals
d = defaults # swap in, assuming not aliased
# if aliased, then instead:
# d.clear()
# d.update(defaults)
if blah is not blah:
d.setdefault(blah, blah)
Cheers,
Chris
--
http://blog.rebertia.com
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar
[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