Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Practical point of view: Why would I want to use Python with C++?

Tags:

c++

python

I've been seeing some examples of Python being used with c++, and I'm trying to understand why would someone want to do it. What are the benefits of calling C++ code from an external language such as Python?

I'd appreciate a simple example - Boost::Python will do

like image 387
Maciek Avatar asked Nov 27 '22 04:11

Maciek


1 Answers

It depends on your point of view:

Calling C++ code from a python application

You generally want to do this when performance is an issue. Highly dynamic languages like python are typically somewhat slower then native code such as C++. "Features" of C++ such as manual memory management allows for the development of very fast libraries, which can then be called from python in order to gain performance.

Another reason is due to the fact that most libraries on both windows and *nix are written in C or C++, and it's a huge advantage to have this existing code base available.

Calling python code from a C++ application

Complex applications sometimes require the ability to define additional abilities. Adding behaviors in a compiled application is messy, requires the original source code and is time consuming. Therefore it's often strategic to embed a scripting language such as python in order to make the application more flexible and customizable.

As for an example: I think you need to clarify a bit what you're interested in if you want the sample to be of any help. The boost manual provides a simple hello world sample, if that's what you're looking for.

like image 162
Emil H Avatar answered Dec 10 '22 11:12

Emil H