[Feature Request] dict.setdefault()

Chris Rebert clp2 at rebertia.com
Mon Apr 11 18:53:45 EDT 2011


On Mon, Apr 11, 2011 at 2:35 PM, rantingrick <rantingrick at 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



More information about the Python-list mailing list