[Python-3000] A request to keep dict.setdefault() in 3.0

Ron Adam rrr at ronadam.com
Tue Jul 10 02:21:31 CEST 2007


Barry Warsaw wrote:
> However, .setdefault() is a horrible name because it's not clear from  
> the name that a 'get' operation also happens.

The return value of .setdefault() could be changed to None, then the name 
would be correct.

And then a helper function could fill the current use case of returning the 
added abject at the same time.

 >>> d = {}
 >>> def setget(setter, getter, vars):
...   setter(*vars)
...   return getter(*vars)
...
 >>> setget(d.setdefault, d.get, ('foo', [])).append(7)
 >>> d
{'foo': [7]}
 >>> setget(d.setdefault, d.get, ('foo', [])).append(8)
 >>> d
{'foo': [7, 8]}

Now if this could be made to be more general so it worked with with other 
objects it might really be useful. ;-)

Cheers,
    Ron


More information about the Python-3000 mailing list