[Python-Dev] Assignment to class (original) (raw)

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): ... > > unsuspectingdict = {1:[1,2],2:3} > > unsuspectingdict.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