Python vs C++

Chris Angelico rosuav at gmail.com
Thu Aug 21 09:40:13 EDT 2014


On Thu, Aug 21, 2014 at 10:54 PM, David Palao <dpalao.python at gmail.com> wrote:
> Why to use C++ instead of python?
>
> This is, perhaps, a bit off-topic, but I really want to know the
> thoughts of experienced python programmers on it.

No, it's a fair question. Why are we all here?

The fact is, there's not a huge amount of reason left. If you're
linking against a C++ API, you may find it easiest to do the whole
program in C++, rather than use something like Cython (which, as far
as I'm aware, is C-only) or write a two-part project. And obviously if
you have an existing C++ codebase, then porting it has costs, and
maintaining it is probably better. But for the most part, I would
strongly recommend starting a project in a high level language like
Python unless there's a really compelling reason to do otherwise. C++
has, of late, been growing a number of features that belong in higher
level languages; but if you want those sorts of features, why not just
grab Python or Pike or something and save yourself the trouble?

One possible advantage is compactness. Once you've compiled a C++
program, you don't have to distribute as much stuff with it as an
entire Python interpreter. Also, true compilation obscures your source
code a lot better than any attempt at a mangled Python would, so if
you're trying to demonstrate to upper management that your code isn't
being given away, that might be an advantage. (But frankly, even that
isn't all that useful. The only way to truly stop people from using
your code is to not give it to them in any form, which these days
generally means putting it on a server and providing web browser
access. And that works just fine for Python code.)

There are reasons for using C, of course. I'm not sure whether your
question is about C++ specifically, or also about C. But in my
opinion, C is for writing high level languages in, and applications
should be written in something else. It's like stack-based programming
- sure, it's efficient and all, but it's not something you want to
spend all day debugging. (If you look at CPython bytecode disassembly,
you'll see that the interpreter's actually stack-based; but we don't
have to worry about keeping the stack straight, we just write nice
clean Python code.) Interfacing with C-level libraries can be done
with an absolute minimum of low-level code (probably with Cython), and
the main application logic can still be in Python.

Let's suppose you're starting a greenfield project, and either Python
or C++ would have been the perfect language, but you chose wrongly.
What are the consequences? If you chose C++, you have a much heavier
development and maintenance cost, and Python would have been good
enough, so you get no significant benefit - basically, you miss out on
the ease of coding that Python offers you. If you chose Python, what
you now have is a proof-of-concept that you can use to prove the
correctness of any rewrites (just get a good test suite going and then
use the same tests for both engines), at a relatively low development
cost. Considering that the cost of erring on Python's side is lower
AND the likelihood of Python being correct is higher, I would say that
you can safely bet on Python for a new project, and leave C++ until
you have some really good reason for taking it up :)

ChrisA



More information about the Python-list mailing list