Default arg for dict() (was Re: Sorting distionary by value)
Emile van Sebille
emile at fenx.com
Thu Mar 28 18:14:50 EST 2002
John Machin
> Python *could* be made like that with the explict dict() constructor
> ... something like:
>
> freq = dict(default=0)
> ...
> freq[word] += 1
Or you could subclass with something like:
>>> class ddict(dict):
... def __getitem__(self, ky):
... return self.setdefault(ky, self.default)
... def __init__(self, default=None):
... self.default = default
...
>>> d = ddict(0)
>>> d['a']+=1
>>> d['a']
1
--
Emile van Sebille
emile at fenx.com
---------
More information about the Python-list
mailing list