converting base class instance to derived class instance

John Roth newsgroups at jhrothjr.com
Tue Feb 10 06:46:19 EST 2004


"François Pinard" <pinard at iro.umontreal.ca> wrote in message
news:mailman.1390.1076365471.12720.python-list at python.org...
[John Roth]
> [François Pinard]

> > >>> o.__class__ = C
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in ?
> > TypeError: __class__ assignment: only for heap types
>
> > By the way, what is a "heap type"?

> I think they're refering to objects that are allocated on the
> heap.  I'm not sure what attempting to instantiate object would do, but
> I suspect the result would be a built-in that can't be modified.

Someone suggested, on this list, that `object()' could be used for
cheaply producing an object which is guaranteed unique, when there is no
need for that object to have any other property.

If you do:

   a = object()
   b = object()
   c = object()
   ...

you will observe that they are all different, none of `a', `b', `c'
compare with `is' to another.  I do not see how the result could be
built-in or pre-allocated.

I could understand that some immutable objects, like 0, "z" or () could
be allocated statically.  But for `object()', I do not see.

[John Roth]
But I believe that object itself is a built-in type, and the error
message in question specified "heap TYPE".

John Roth

-- 
François Pinard   http://www.iro.umontreal.ca/~pinard





More information about the Python-list mailing list