This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author rhettinger
Recipients
Date 2006-02-21.21:03:32
SpamBayes Score
Marked as misclassified
Message-id
In-reply-to
Content
Logged In: YES 
user_id=80475

I just had one other thought.  If the subclass implemented 
__getitem__ and on_missing directly, then there would be 
no need to make any changes to dictobject.c and we could 
leave the mapping API alone (for a less disruptive 
implementation):

class defaultdict(dict):
    default_factory = None
    def __getitem__(self, key):
        try:
            return dict.__getitem__(self, key)
        except KeyError:
            return self.on_missing(self, key)
    def on_missing(self, key):
        if self.default_factory is None:
            raise KeyError(key)
        value = self[key] = self.default_factory(key)
        return value

Anyone needing to hook the on_missing method can do it 
through a subclass of defaultdict rather than dict.

There are two side benefit to this approach.  One, we keep 
the changes all in one object making it easier to 
understand than putting framework methods in the parent.  
Two, this also allows the on_missing call to be done 
directly instead of through PyEval_CallMethod().  That 
will provide a nice speed-up for the common cases where 
defaultdict hasn't been subclassed.
History
Date User Action Args
2007-08-23 15:45:48adminlinkissue1433928 messages
2007-08-23 15:45:48admincreate