Maciek
Maciek

Reputation: 19893

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

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

Upvotes: 4

Views: 656

Answers (7)

Mark Rushakoff
Mark Rushakoff

Reputation: 258388

Here's a real-life example: I've written a DLL in C to interface with some custom hardware for work. Then for the very first stage of testing, I was writing short programs in C to verify that the different commands were working properly. The process of write, compile, run took probably 3-5 times as long as when I finally wrote a Python interface to the DLL using ctypes.

Now, I can write testing scripts much more rapidly with much less regards to proper variable initialization and memory management that I would have to worry about in C. In fact, I've even been able to use unit testing libraries in Python to create much more robust tests than before. Would that have been possible in C? Absolutely, but it would have taken me much longer, and it would have been many more lines of code.

Fewer lines of code in Python means (in general) that there are fewer things with my main logic that can go wrong.

Moreover, since the hardware communication is almost completely IO bound, there's no need to write any supporting code in C. I may as well program in whatever is fastest to develop.

So there you go, real-life example.

Upvotes: 2

Paul Kelly
Paul Kelly

Reputation: 4019

One nice thing about using a scripting language is that you can reload new code into the application without quitting the app, then making changes, recompile, and then relaunching the app. When people talk about quicker development times, some of that refers to this capability. A downside of using a scripting languages is that their debuggers are usually not as fully featured as what you would have in C++. I haven't done any Python programming so I don't know what the features are of its debugger, if it has one at all.

This answer doesn't exactly answer what you asked but I thought it was relevant. The answer is more the pro/cons of using a scripting language. Please don't flame me. :)

Upvotes: 0

Emil H
Emil H

Reputation: 40230

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.

Upvotes: 21

LeafGlowPath
LeafGlowPath

Reputation: 3799

  1. Performance :

From my limited experience, Python is about 10 times slower than using C. Using Psyco will dramatically improve it, but still about 5 times slower than C. BUT, calling c module from python is only a little faster than Psyco.

  1. When you have some libraries in C.
    For example, I am working heavily on SIP. It's a very complicated protocol stacks and there is no complete Python implementation. So my only choice is calling SIP libraries written in C.

There are also this kind of cases, like video/audio decoding.

Upvotes: 0

Daniel Earwicker
Daniel Earwicker

Reputation: 116714

Because C++ provides a direct way of calling OS services, and (if used in a careful way) can produce code that is more efficient in memory and time, whereas Python is a high-level language, and is less painful to use in those situations where utter efficiency isn't a concern and where you already have libraries giving you access to the services you need.

If you're a C++ user, you may wonder why this is necessary, but the expressiveness and safety of a high-level language has such a massive relative effect on your productivity, it has to be experienced to be understood or believed.

I can't speak for Python specifically, but I've heard people talk in terms of "tripling" their productivity by doing most of their development in it and using C++ only where shown to be necessary by profiling, or to create extra libraries.

If you're a Python user, you may not have encountered a situation where you need anything beyond the libraries already available, and you may not have a problem with the performance you get from pure Python (this is quite likely). In which case - lucky you! You can forget about all this.

Upvotes: 3

Paul McMillan
Paul McMillan

Reputation: 20117

Generally, you'd call C++ from python in order to use an existing library or other functionality. Often someone else has written a set of functions that make your life easier, and calling compiled C code is easier than re-writing the library in python.

The other reason is for performance purposes. Often, specific functions of an otherwise completely scripted program are written in a pre-compiled language like C because they take a long time to run and can be more efficiently done in a lower-level language.

A third reason is for interfacing with devices. Python doesn't natively include a lot of code for dealing with sound cards, serial ports, and so on. If your device needs a device driver, python will talk to it via pre-compiled code you include in your app.

Upvotes: 5

Andy J Buchanan
Andy J Buchanan

Reputation: 1950

Here's two possibilities:

  1. Perhaps the C++ code is already written & available for use.
  2. It's likely the C++ code is faster/smaller than equivalent Python

Upvotes: 3

Related Questions