converting base class instance to derived class instance
François Pinard
pinard at iro.umontreal.ca
Mon Feb 9 17:21:04 EST 2004
[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.
--
François Pinard http://www.iro.umontreal.ca/~pinard
More information about the Python-list
mailing list