[Feature Request] dict.setdefault()

R

rantingrick

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. 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)

....nuff said.

PS: And to counter the very *extemely* likely chance of some smart
arse responses
* YES, i know i could create my own setdefault method but that is
not the point.
* I know we are under the moratorium but someone had to mention it,

--rr
 
W

Westley Martínez

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. 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)

...nuff said.

PS: And to counter the very *extemely* likely chance of some smart
arse responses
* YES, i know i could create my own setdefault method but that is
not the point.
* I know we are under the moratorium but someone had to mention it,

--rr

Go to bugs.python.org
 
M

MRAB

Go to bugs.python.org
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.
 
C

Chris Angelico

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) ?

Chris Angelico
 
R

Raymond Hettinger

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. 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)

I hate to feed a troll, but I'm curious when you see that pattern of
calls. The typical use case always makes immediate use of the value
returned by setdefault():

d.setdefault(key, []).append()

The pattern you've shown would more typically be expressed like this:

for key in multiple_defaults.items():
if key, default_value not in d:
d[key] = default_value

Or people sometimes use updates to get the same effect:

result = multiple_defaults.copy()
result.update(d)

Another approach is to use something like ChainMap() which has been
added to Python 3.3. http://docs.python.org/dev/library/collections.html#collections.ChainMap

result = ChainMap(d, multiple_defaults)


Raymond
 
C

Chris Rebert

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
 
M

MRAB

Wouldn't x.updatedefault(y) be pretty much y.update(x) ?
I suppose it would, except that it wouldn't be in-place as such, and it
wouldn't be as efficient if you're wanting to default only a few
entries in a larger dict.
 
T

Tim Chase

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
 
R

Raymond Hettinger

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

Guido rejected merge() a long time ago.

Anyway, there is a new ChainMap() tool in the collections module for
Py3.3 that should address a number of use cases for handling default
values.


Raymond

twitter: @raymondh
 
R

rantingrick

A new method like "updatedefault" may be better, IMHO. It would act
like "update" except that it wouldn't overwrite existing values.

That's sounds good MRAB! After you mentioned this i had an epiphany...
why not just add an extra argument to dict.update?
 
T

Thomas Rachel

Am 12.04.2011 04:58, schrieb rantingrick:
That's sounds good MRAB! After you mentioned this i had an epiphany...
why not just add an extra argument to dict.update?

This is AFAICS inconsistent to the possibility to do dict.update(a,
k1=v1, k2=v2).

Then you cannot set the key clobberexistingvalues to False via the
update method any longer...


Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top