Python wrapper for C++ core

Carl Banks pavlovevidence at gmail.com
Wed Oct 18 08:37:10 CEST 2006


holly.woodland at gmail.com wrote:
> Hi All
>
> Apologies in advance for the pretty basic question - but I can't seem
> to find an answer anywhere else.
>
> I am developing a fluid sim in C++ and have heard that many people use
> Python in conjunction with C++ for this sort of thing (especially in
> games dev).
>
> What I can't understand why you would want to do this. Obviously the
> core would be written in C++ (for my purposes anyway) so what parts
> would be written in Python? What is the benefit, in this situation, of
> using Python instead of C++?

I see "fluid sim" and "games dev" in the same post, which kind of makes
me scratch my head.  What would you need fluid sim for in a game?
Maybe it's a flight sim; I suppose now computers are fast enough to
attempt a low-fidelity CFD (computational fluid dynamics) in real time.
 (Or maybe you mean something different by fluid sim, like a puzzle
game involving valves or something.)

Anyways, combining Python with C is useful for all kinds of
applications, not just games and simulations, because Python has
different strengths than C.  C is faster and can inteface easily with
hardware and the OS; Python is better at pretty much everything else.
It takes care of a lot of stuff you'd have to do yourself in C.

Speaking as someone who studied a lot of CFD in college, I think Python
in particularly is very useful for extreme numerical work.  (If that's
not what you're talking about, never mind the rest of the post.)

The people who do fluid simulations are engineers (classical sense),
not programmers.  These people do NOT like to program.  They like to
fiddle with arrays and do arithmetic; stuff like memory management,
input/output, and other computer-science-ish tasks are a pain in the
neck.  Time spent doing that kind of stuff is time not spent doing
useful things like hand-optimizing the hell out of the numerical code.

Python (even with Numeric) is simply too slow for high-end CFD
applications, so the numerical part is almost always written in C or
Fortran.  But C and Fortran are terrible languages to do things like
memory management and I/O in.  Python, OTOH, is terrific for these
things.  It's a great language for people who don't like to program.
Comparable languages such as Ruby are also good for I/O and stuff, but
I think Python's the best of all for numerical work:

1. Large support community
2. Large standard library and lots of third-party packages out there,
addresses all kinds of needs
3. A very spiffy package, pyfort, to interface with Fortran, which can
also works for C functions that act like Fortran (which is often the
case in a numerical application).  Relatively straightforward general
interface to C if pyfort won't do.
4. numpy, a very good, well-maintained package which allows Python
programs to operate on arrays directly (useful for tasks that don't
need to be hyper-optimized).
5. Superficially similar to C and Fortran, which is no small thing for
engineers


Carl




More information about the Python-list mailing list