I have a bunch of objects in the ZODB that I need to convert to other objects. The new object have the same meta_type and the same attributes. But they are of a new class. So I thought that I could simply assign a new class. I tried assigning the new class to __class__ but that didn't work. Like:: obj.__class__ = SomeNewClass I suspect that is because I am assigning it to an aquisition wrapper. But I am not shure. Would I need to do something like? base = obj.aq_base base.__class__ = SomeNewClass -- hilsen/regards Max M, Denmark http://www.mxm.dk/ IT's Mad Science
For me it's a problem with ExtensionClasses and, to me, there is no way to make it work. In [11]: from ExtensionClass import Base In [12]: class r(Base) : pass ....: In [13]: class e : pass ....: In [14]: d.__class__ Out[14]: <extension class __main__.r at 82b9cf0> In [15]: d.__class__ = e In [16]: d.__class__ Out[16]: <extension class __main__.r at 82b9cf0> In [17]: class e(Base) : pass ....: In [18]: d.__class__ = e In [19]: d.__class__ Out[19]: <extension class __main__.r at 82b9cf0> I will use the good pattern if it's a piece of your conception to work with 'objects that mutate to other objects', but what do you really need (i.e. it is not the good way to upgrade products, see the ZDG - Chapter 3: Zope Products - Evolving Products) ? Max M a écrit :
I have a bunch of objects in the ZODB that I need to convert to other objects.
The new object have the same meta_type and the same attributes. But they are of a new class.
So I thought that I could simply assign a new class.
I tried assigning the new class to __class__ but that didn't work. Like::
obj.__class__ = SomeNewClass
I suspect that is because I am assigning it to an aquisition wrapper. But I am not shure.
Would I need to do something like?
base = obj.aq_base base.__class__ = SomeNewClass
Max M wrote at 2004-5-13 09:30 +0200:
I have a bunch of objects in the ZODB that I need to convert to other objects.
The new object have the same meta_type and the same attributes. But they are of a new class.
So I thought that I could simply assign a new class.
I tried assigning the new class to __class__ but that didn't work. Like::
obj.__class__ = SomeNewClass
I suspect that is because I am assigning it to an aquisition wrapper. But I am not shure.
Would I need to do something like?
base = obj.aq_base base.__class__ = SomeNewClass
It will not work. For efficiency reasons, the class is not coded only in the object itself but also in references to the object. When an object is loaded, it is (usually) loaded via a reference and this reference determines the class. Thus, you must not only change the object's class. You must also change all references to the object. Probably, you must also tell the object that is has been modified. -- Dieter
participants (3)
-
Dieter Maurer -
Maric MICHAUD -
Max M