Python2.1/ExtensionClass: Changing the __class__

Sebastien Bigaret Sebastien.Bigaret at inqual.com
Fri Aug 30 07:51:12 EDT 2002


> Yes you can assign to __class__ and it works often.
> The exception: not if you are assigning a new style class to the
> .__class__  of an instance object of an old style class.
> I did post about this here on c.l.p. on August 10 and there is a mixin
> function there too and a short demo program.  Also posted a bug report
> and it seems they are working on it.  Title of the post was "Assigning
> to __class__".

Thanks for your answer. I've seen your message --unfortunately this is for
python2.2. Python2.2 seems ok to assign __class__ to an instance of an
extension class such as ZODB's Persistence (given that the two classes are
compatible, of course), but py2.1 does not:

    >>> import ZODB
    >>> from Persistence import Persistent
    >>> class A(Persistent):
    >>>   pass
    >>> 
    >>> class B(A):
    >>>   pass
    >>> 
    >>> a=A()
    >>> dir(a)
    []
    >>> a.__class__
    <extension class __main__.A at 849c550>
    >>> a.__class__=B
    >>> a.__class__
    <extension class __main__.A at 849c550>
    >>> dir(a)
    ['__class__']

This is not directly bound to your problem, where you cannot assign a `new
class' to __class__ of an `oldclass' 's instance: here I can assign it, but it
has not effect (and, BTW, I cant see no more solution for your problem than
for mine).


> For-now-lets-just-try-not-to-do-too-much-hacking-ly-yrs

I hope I could live without it! I guess I should think of refactoring the
stuff and abandoning assignment to __class__, just as you suggest for you.


-- Sébastien.





More information about the Python-list mailing list