[Zope] Changing a Z object's __class__ dynamically

Hung Jung Lu hungjunglu@hotmail.com
Sat, 19 Aug 2000 10:46:09 PDT


Hi,

One powerful feature of Python is the ease of reloading modules. This makes 
debugging extremely nice, since you can modify the code, reload the module, 
and all this while the main program is still running.

Moreover, if a particular instance object has been created, you can actually 
edit the object's class code, reload the module, and change the class of the 
object to the modified class. Something like:

    my_module = sys.modules['module_name']
    reload(my_module)
    my_new_class = my_module.my_class
    my_instance.__class__ = my_new_class

This works all very nicely in pure Python. But not in Zope. When the object 
is a Zope object, somehow the last statement does not seem to do anything. 
That is, the object's class is not updated.

I don't know what exactly is going on. My conjectures are: (1) Digicool 
people have re-written Python's system function setattr(), so that when it 
is an Z Object, the __class__ cannot be changed. (2) Caching: when I change 
the class of a Z Object, somehow it does not reflected in the Z Object's 
memory-cached version.

At any rate, this is really surprising. Since usually when you do:

    x = 3
    print x

you would expect the result to be 3. But with a Z object's __class__, you 
can assign it to something, and then nothing happens. That is:

    x.__class__ = new_class
    print x.__class__

and it will print the old class, not the new class.

Does anyone know what I can do to change the class of a Z object 
dynamically? This will make the development of Zope products a lot easier. 
(Of course, all these reloading features are extremely dangerous, so it's 
best to use them only for development and strip them from the code at the 
end.)

regards,

Hung Jung

________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com