Interchangable programing Python and C++

In order to have more flexibility and more coding convenience can we use C++ and Python interchangably, That is, whenever program requires in depth skills and is more easy with low-level language to use C++ and whenever it is not requiring any in depth knowledge to switch back to more flexibile language such as Python. Doing all this at the same source file or combining the two source files e.g. *.py and *.cpp to produce the one result, do we have such flexibility?

Regards,

I don’t know the details, but yes you can mix c or cpp with panda. There are a few options that I know of. The choice is going to depend on the structure of your code:
[list=]
[]Use interrogate (panda’s build system) for c++
[
]Use CFFI
[]CPython C modules
[
]Cython
[/list]

Any pseudo-code will be appreciated.

Python can call C code. This is not so difficult and is desirable because C has the potential to execute faster. I think Panda already does this with the lower level stuff. I have used it in the past to calculate CRCs.

docs.python.org/2.7/extending/extending.html

C/CPP can call Python scripts. This is not so easy to get it work 100% correctly and is error prone. At least that was my experience.

docs.python.org/2/extending/embedding.html

I’ve never used either with Panda3D but I’m sure its possible.

I don’t have any pseudo code other than what I listed on the project websites.
May I suggest writing everything in python or cpp, whichever you are most comfortable with, and then tackle the multi-language issue later. Chances are python will be fast enough for most of your code – you can optimize it in c or c++ after you profile your project if you find bottle necks.

FYI, I made a tool to compile c++ extensions for python/panda3d. You can find it here: https://github.com/tobspr/P3DModuleBuilder

Basically you put your c++ code into a folder, and the tool will compile it into a .pyd/.so file which you can then import as a python module. To export your classes, simply put the methods you’d like to export below a “PUBLISHED:” section (instead of “public:”), for example like https://github.com/tobspr/RenderPipeline/blob/master/rpcore/native/source/ies_dataset.h#L47

That way you can write performance dependent code in C++, and use it in python. Of course its not as convenient as having all of your code in a single source, though.