Default arg for dict() (was Re: Sorting distionary by value)

John Machin sjmachin at lexicon.net
Thu Mar 28 21:41:14 EST 2002


"Emile van Sebille" <emile at fenx.com> wrote in message news:<a808fj$ooe50$1 at ID-11957.news.dfncis.de>...
> 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
> ...

You could indeed. However I'm interested in dicts with hundreds of
thousands of entries and millions of accesses. How much extra (a) time
and (b) memory would be taken up with this approach (subclassing in
Python), compared with the approach of of building it into the core?
My answers would be (a) lots (b) don't know, either minimal or lots.
Will do some experimentation later.



More information about the Python-list mailing list