copy-on-write for dict objects?

Matthias Oberlaender matthias.oberlaender at REMOVE.daimlerchrysler.com
Thu Jan 16 16:35:01 CET 2003


In <7h34r89p3wp.fsf at pc150.maths.bris.ac.uk> Michael Hudson wrote:

<snip>  my stuff deleted 

> 
> You *could* try this (untested):
> 
> class UnSharedDict(dict):
>    __slots__ = ["other"]
> 
> class SharedDict(UnSharedDict, UserDict.DictMixin):
>    __slots__ = ["other"]
>    def __init__(self, other):
>        self.other = other
>    def __getitem__(self, key):
>        return self.other[key]
>    def __setitem__(self, key, value):
>        super(SharedDict, self).__setitem__(key, value)
>        self.__class__ = UnSharedDict
> 
>    def has_key(self, key):
>        return key in self.other
>    def __iter__(self):
>        return iter(self.other)
> 
> But you're probably going to find lot's of places that blissfully
> ignore the __getitem__ method.
> 
Another problem with all these approaches is that they introduce a level of 
indirection. For example, I use to write 'k in d' in my code (which seems to 
be even faster than 'd.has_key(k)'.  If this has to go through a method of my 
own, the speed penalty is significant.

--
 ____  __  _/_/ . 
( / / ( /  / / /  

=====================================================================
Matthias Oberlaender, DaimlerChrysler AG, Research Center Ulm
RIC/AP (Machine Perception)
Wilhelm-Runge-Str. 11,  P.O. Box 2360,  89013 Ulm, Germany
Phone: +49 731 505 2354       Fax: +49 731 505 4105
Email: matthias.oberlaender at REMOVE.daimlerchrysler.com
=====================================================================





More information about the Python-list mailing list