Including Python

I’m open to using pythong for parts of my app, but I’d still like the majority of my code to be C++ (I have my reasons). Is it possible for me to only do certain things, like GUI stuff, in Python, but still do the rest in C++? If so, how do I call the python code from within my C++ application?

Look to the Python/C API section of the python.org website for the full answer to questions like this.

The short answer is, to call into Python, you have to get a pointer to the Python function you want to call, then call a function such as PyObject_CallObject() on that Python function pointer.

But you may find it easier to let Python be the main loop, and C++ just Python’s servant. Then all of your calls will be from Python into C++, and not the other way around, which is a little easier (and there are lots of options to solve this problem, including interrogate or SWIG).

David

Thanks. I found the API, and it looks like it has what I need (and much more).

Is there a short answer as to why going from Python to C++ is easier than the other way around? I have my application all set up in an IDE that I like, and I’m a little hesitant to make a move.

The short answer is that that is the more common usage pattern, and there are more tools that are designed to help you go that way. If you go from C++ to Python, you have to write a lot of wrapper code yourself. But if you’re used to using C++, you’re probably used to writing a lot of wrapper code for stuff, so maybe you won’t notice. :wink:

David