[Python-Dev] Assignment to __class__
Kevin Jacobs
jacobs@penguin.theopalgroup.com
Fri, 10 Jan 2003 08:04:28 -0500 (EST)
On Fri, 10 Jan 2003, Guido van Rossum wrote:
> > Sorry, I was not as clear as I should have been. Here is what used to work,
> > and I hope can be made to work again:
> >
> > class AlgebraicDict(dict):
> > def doReallyComplexThings(self, foo): ...
> > def __add__(self, other): ...
> > def __mul__(self, other): ...
> >
> > unsuspecting_dict = {1:[1,2],2:3}
> >
> > unsuspecting_dict.__class__ = AlgebraicDict
> > > TypeError: __class__ assignment: only for heap types
>
> But in this case, the instance layout of dict and AlgebraicDict is
> different anyway (AlgebraicDict has room for the __dict__ to contain
> instance variables) so you can't do that.
>
> The layouts could be the same if you add __slots__ = [] to
> AlgebraicDict.
Again, I was not 100% clear. AlgebraicDict and AlgebraicList do define
__slots__ to be an empty sequence.
> But the problem is that even then, AlgebraicDict may be allocated
> using a different free list than dict, and changing its __class__ to
> dict would free it using the wrong free list.
>
> To work around, create a neutral dict subclass that has the same
> layout at AlgebraicDict.
The problem is that I do not control where the dict is being allocated. I
suppose I can live with having to make copies, but it seems like a major
step backwards to me.
Thanks,
-Kevin
--
Kevin Jacobs
The OPAL Group - Enterprise Systems Architect
Voice: (216) 986-0710 x 19 E-mail: jacobs@theopalgroup.com
Fax: (216) 986-0714 WWW: http://www.theopalgroup.com