difference 2.3 versus 2.2

"Martin v. Löwis" martin at v.loewis.de
Thu Jan 23 03:05:16 EST 2003


Michele Simionato wrote:
> TypeError: __class__ assignment: only for heap types
> 
> Can somebody put some light on this TypeError ? 
> It looks like intentional.
> 
> I can change class of a normal object, why not the class of a class ?

You can't change the class of arbitrary normal objects:

 >>> (2).__class__=float
Traceback (most recent call last):
   File "<interactive input>", line 1, in ?
TypeError: __class__ assignment: 'float' object layout differs from 'int'

In 2.3, this rules was tightened that not only the layout must be 
compatible, but both old and new type must be "heap types", i.e. created 
through a class statement. Without this restriction, 2.3 would have 
allowed to write

(2).__class__ = bool

which would have cheated the constructor of the bool type.

In your example, the old type of the object is TypeType, which is not a 
heap type.

Regards,
Martin





More information about the Python-list mailing list