magical expanding hash

Steven D'Aprano steve at REMOVETHIScyber.com.au
Wed Jan 18 06:23:29 EST 2006


On Tue, 17 Jan 2006 18:00:00 -0700, Steven Bethard wrote:

> Steve Holden wrote:
>> Steven Bethard wrote:
>>> Agreed.  I really hope that Python 3.0 applies Raymond Hettinger's 
>>> suggestion "Improved default value logic for Dictionaries" from
>>>      http://wiki.python.org/moin/Python3%2e0Suggestions
>>>
>>> This would allow you to make the setdefault() call only once, instead 
>>> of on every lookup:
>>>
>>>      class meh(dict):
>>>          def __init__(self, *args, **kwargs):
>>>              super(meh, self).__init__(*args, **kwargs)
>>>              self.setdefault(function=meh)
>>>
>>> STeVe
>>>
>> In fact, why not go one better and also add a "default" keyword 
>> parameter to dict()?
> 
> It's not backwards compatible:
> 
>  >>> dict(default=4)
> {'default': 4}
> 
> And I use the **kwargs form of the dict constructor often enough to hope 
> that it doesn't go away in Python 3.0.

I don't like the idea of all dicts having default values. Sometimes you
don't want a default value, you want an exception when the key isn't in
the dict.

And even if you do want defaults, sometimes you want a default which is
global to the dict, and sometimes you want a default which depends on the
key. More of a "missing value" than a default.

I vote to leave dict just as it is, and add a subclass, either in a module
or as a built in (I'm not fussed either way) for dicts-with-defaults.



-- 
Steven.




More information about the Python-list mailing list