Python Question: Morphing Classes?
In lack for other Python mailing lists I send my question here (there ought to be someone with a bit of python-zen here ;-) I'm have a bit of a problem because I need to copy one object in the ZODB. Now the copy-and-paste support of Zope will not do it because it actually export/imports the object including all its sub object. So I have abandoned that idea (after an interesting attempt to export only the object (based on their oids), which was bound to fail. Anyway, there isn't any generic way to create an arbitrary product instance. Because if I could do that I could copy all the data into the new object, but I need to be able to handle any addable Zope object and they all got different init-methods requiring arguments. But I have tried another thing: Creating an instance (a) of the class C: pass, I can set the a.__class__=A (where A is another class), a will report that it is a instance of A and it will have all of A's class context. This look like a possible, even if not so less hackerish, way around my problem. Are there any traps I could fall into here? Regards, Johan Carlsson -- Torped Strategi och Kommunikation AB Johan Carlsson johanc@easypublisher.com Mail: Birkagatan 9 SE-113 36 Stockholm Sweden Visit: Västmannagatan 67, Stockholm, Sweden Phone +46-(0)8-32 31 23 Fax +46-(0)8-32 31 83 Mobil +46-(0)70-558 25 24 http://www.easypublisher.com http://www.torped.se
At 23:57 2002-12-09 +0100, Johan Carlsson [EasyPublisher] wrote:
Creating an instance (a) of the class C: pass, I can set the a.__class__=A (where A is another class), a will report that it is a instance of A and it will have all of A's class context.
This look like a possible, even if not so less hackerish, way around my problem.
Are there any traps I could fall into here?
I just found an even better (or at least more working way) to do this: obj = new.instance(Class, Dict) The problem is that new.instance doesn't seam to work with Extension Classes? Any idea? Johan Carlsson -- Torped Strategi och Kommunikation AB Johan Carlsson johanc@easypublisher.com Mail: Birkagatan 9 SE-113 36 Stockholm Sweden Visit: Västmannagatan 67, Stockholm, Sweden Phone +46-(0)8-32 31 23 Fax +46-(0)8-32 31 83 Mobil +46-(0)70-558 25 24 http://www.easypublisher.com http://www.torped.se
Johan Carlsson [EasyPublisher] writes:
At 23:57 2002-12-09 +0100, Johan Carlsson [EasyPublisher] wrote:
Creating an instance (a) of the class C: pass, I can set the a.__class__=A (where A is another class), a will report that it is a instance of A and it will have all of A's class context.
This look like a possible, even if not so less hackerish, way around my problem.
Are there any traps I could fall into here?
I just found an even better (or at least more working way) to do this:
obj = new.instance(Class, Dict)
The problem is that new.instance doesn't seam to work with Extension Classes? Indeed.
Use "__basicnew__" for this. Have a look at "pickle.load" to learn how it is used. Dieter
Johan Carlsson [EasyPublisher] writes:
.... But I have tried another thing: Creating an instance (a) of the class C: pass, I can set the a.__class__=A (where A is another class), a will report that it is a instance of A and it will have all of A's class context. "ExtensionClass" instances do not allow you to write to the "__class__" attribute. I think, they just ignore it.
Dieter
participants (2)
-
Dieter Maurer -
Johan Carlsson [EasyPublisher]