[C++-sig] Re: Accessing C++ values from within Python

Jeffrey Holle jeff.holle at verizon.net
Wed Jun 30 09:29:59 CEST 2004


I'm sending the source of a simple project that does what I believe you 
are trying to do.

In Main.cpp, note the use of call, and the use of ref within it.
The former does all the "magic" of converting C++ objects to/from python.
The later causes the C++ object to be passed by reference, allowing the 
python script to alter C++ object attributes.


xin wrote:
> Hi.
> I have a setup such as the following:
> 
> class object {
> public:
>     void doSomthingElse();
>     float x;
>     float *y;
>     Somthing SomePyScript;
> };
> 
> int main()
> {
>     object *array = new object[10]; // size of array is only an example
>     for (int i = 0; i < 10; i++)
>     {
>         array[i].y = new float[array[i]]
>         RunPythonScript(array[i]);
>     }
>     return 1;
> }
> 
> Where:
>  - " Somting SomePyScript; " Is some reference to a python script that 
> is affiliated with this instance of the class (varies from instance to 
> instance)
>  - " RunPythonScript(array[i]); " Is some method of running the python 
> script that is affiliated with the instance array[i].
> 
> My intention is that the python scripts change values within the class 
> 'object'
> e.g. The python code sets 'x' to equal 0.9. And then sets each value of 
> y to equal a muliple of 9. (y[0] = 0, y[1] = 9, y[2] = 18, ... ,y[100] = 
> 900 etc...)
> 
> Is this at all possible? Is it possible without moving all the memory 
> from one place, to another area accessable by both python and then back 
> again? If so how would I go about doing it?
> 
> I've tried all sorts of methods of attempting this, but have had no 
> luck. Any help would be appreciated.
> 
> Thanks.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: extended_embeddedGreeter.tar
Type: application/x-tar
Size: 20480 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20040630/78f7b668/attachment.tar>


More information about the Cplusplus-sig mailing list