difference 2.3 versus 2.2
mis6 at pitt.edu
Thu Jan 23 17:00:45 CET 2003
"Martin v. Löwis" <martin at v.loewis.de> wrote in message news:<b0o7nr$t1l$07$1 at news.t-online.com>...
>>Michele Simionato wrote:
>>Python 2.3a1 (#1, Jan 6 2003, 10:31:14)
>>[GCC 2.96 20000731 (Red Hat Linux 7.2 2.96-108.7.2)] on linux2
>>Type "help", "copyright", "credits" or "license" for more information.
>>>> class Meta(type): pass ...
>>>> class C(object): pass ...
>> 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
Therefore, if I understand correctly, 'heap type' means 'user-defined type',
as opposed to built-in typ). For instance, I see that I *can* change the
metaclass from Meta1 to Meta1 if both Meta1 and Meta2 are user-defined:
>>> class Meta1(type): pass
>>> class Meta2(type): pass
This restriction (i.e. it is possible to change the __class__ attribute
only for heap types) should be reported in "What's new in Python 2.3",
since it is a change that breaks old code. I will send a copy of the
present mail to python-docs at python.org.
More information about the Python-list