[Python-Dev] Assignment to __class__
Kevin Jacobs
jacobs@penguin.theopalgroup.com
Fri, 10 Jan 2003 06:40:52 -0500 (EST)
On Thu, 9 Jan 2003, Guido van Rossum wrote:
> You can only set __class__ when the old and new class instance have
> the same instance layout at the C level. Changing this is impossible
> given the way objects are implemented in C. This means you can never
> change a list into a dict or vice versa, because the C structs are
> different.
>
> Or do I misunderstand you? Can you give an example of something you
> think should be allowed but currently isn't?
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
Analogously, we want to transform native lists into AlgebraicLists, which of
course has list as a base class.
Hope this clears things up,
-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