Using python for dynamic behavior from C++

programmer.py at gmail.com programmer.py at gmail.com
Wed Aug 15 10:39:08 EDT 2007


On Aug 15, 8:35 am, "Jorgen Bodde" <jorgen.maill... at gmail.com> wrote:
> Hi all,
>
> I am looking into using Python to introduce dynamic behavior in my
> C++, e.g. something like a simulation where objects can interact with
> eachother. I know Python can be called from C++, but is it possible to
> call a binary compiled Python file / array from C++ ? The reason I ask
> is that if my objects call a script every tick to do something, I
> would not like to let the python interpreter recompile the same code
> over and over again.
>
> Any pointers to how I can do that?
>
> Regards,
> - Jorgen

Someone has already mentioned the documentation (http://
docs.python.org/ext/ext.html).  But in a nutshell, your code is not
`recompiled` everytime you send it to the interpreter.  Assuming you
put your code into a module, you'll do things that look a lot like
python, but only C -- for instance you'll actually import your python
module with the C function PyImport_Import.

After dancing with the guts of python for a bit, you'll really
appreciate all the magic that goes on under the hood when you type
python.  I know I did!

There are also tools to help extend python.  I like swig. (http://
www.swig.org/).  You're embedding, but hopefully you'll wise up and
realize it's better to write your app in python and drop to C when you
absolutely, positively need speed ;).

jw




More information about the Python-list mailing list