[C++-sig] Creating an instance of a python class derived from a C++ object from C++
ingo at fargonauten.de
Sat Jul 10 20:56:47 CEST 2004
Am Sa, den 10.07.2004 schrieb Brian Hall um 19:47:
> But I want to do this from C++, not Python... how would I do this? Basically
> I want to be able to define new Python derived types of MyCppClass at runtime
> on the fly, and redefine them at will. Say for instance the user of the
> application typed in the above declaration of MyPythonClass... and then later,
> updated that definition to return 43 instead, I want to be able to redefine
> the code for the class... if I did that, would I have to destroy the old
> instance and create a new one? Any ideas?
As far as I know, you have to use the Python/C API directly for that.
See the posting by Murray Cumming ("evaluating multiple statements") and
my answer from tuesday. I never tried it, but you should be able to use
that procedure repeatedly with the same module name. If you try that,
please report back if you had to do anything, I will have to do
something like that in the near future.
To create an object of class "MyPClass", use the method "MyPClass" from
the module dictionary. After reloading, you will have to recreate your
objects to reflect the new code. If the state is important, you might
want to pickle them.
Soll doch jeder bleiben, wie er gerne wäre.
More information about the Cplusplus-sig