Converting Python app to C++ completely

Joseph A. Knapka jknapka at earthlink.net
Thu Aug 29 20:17:23 CEST 2002


Alan James Salmoni wrote:
> 
> Hi all,
> 
> I am *very* sorry is this has been answered before (I'm sure it must
> have at some point), but trawling Google Groups hasn't shown anything
> completely relevant to my question.
> 
> I have written an application (a package for statistical analysis -
> SalStat) using Python and wxPython, not just because I wanted to, but
> also to learn the language. I found both Python & wxPython to be superb
> for the task.
> 
> Now, what I want to do is write the thing in C++. The reason? heh,
> because I want to ;) Well, actually I want to learn C++ too, and I would
> like to have the performance of a compiled program for larger data sets
> (just for fun and comparison more than anything).
> 
> I know very little about C++, but have a few questions about converting
> from Python code. I am asking here because Pythonistas are more likely
> to know C++ than vice versa (and CLP is one of the friendliest groups
> around - creep, creep).

Hey guys, open up the dungeon, we've got another one! Now where
did I put that white-hot pair of pliers? ...
 
> 1) How easy is it to convert existing Python code to C++? I read that
> each line of C++ code is roughly equivilent to 5-8 lines of C++ code,

Yes, that's what enables us to write 1-line C++ programs that
do absolutely anything :-) I assume you meant "each line of
Python". I'd say an 8:1 ratio is actually a little low for
short programs, but the larger the project, the better the
ratio will get in favor of C++, if you do your abstraction
properly. You will *always* write more lines of C++ than
of Python to accomplish a given task, however.

> but is the conversion a simple task or is it troublesome?

I would say it is not a mechanical transformation. It will require
a lot of thought, and a lot of code. You'll be amazed at all the
stuff Python does for you that you'll have to handle yourself
in C++.
 
> 2) Are there any pitfalls to beware of - I understand that dealing with
> memory and pointers might be a problem, but like I said earlier, I just
> don't really know?

Memory management is, I'm sure, the single biggest source of fatal
errors in C++ code. Of course, in Python you don't have to deal
with it at all :-) In C++, every bit of dynamic memory you use is
pretty much your responsibility to clean up, and of course you
must be careful not to overwrite the bounds of memory blocks,
free a memory block twice, forget to free a memory block, use
a memory block after it's been freed, use memory that you haven't
explicitly allocated, etc., etc. We C++ programmers have developed
tricks to help us deal with this sort of thing, in much the same
way that people who suffer severe childhood trauma develop
psychological mechanisms to insulate themselves from those
experiences :-) You should get a copy of Scott Meyers's books
"Effective C++" and "More Effective C++", along with the
latest edition of Stroustrup's "The C++ Programming Language"
that you can find. Unlike Python books, C++ books are almost
universally crap (they will lead you down hellbound paths
by "forgetting" to tell you about really important corners of
the language); the Meyers and Stroustrup books are among the
exceptions. I suspect Eckel's "Thinking in C++" would be good
too, but I haven't read it.

> 3) Does C++ have equivilent functional constructs like reduce and map?
> These helped to make my code much cleaner, tighter and faster, and I
> would miss them dearly.

Yes. The Standard Template Library will help you here. But
you're buying yourself quite a steep learning curve.
 
> 4) wxWindows - From what I can understand from the wxWindows
> documentation, the conversion should be relatively straightforward, but
> I would appreciate a more experienced opinion.

No comment.
 
> 5) Is there a free C++ compiler for Mac OS8/9? I am going to use GCC for
> every other platform (the Python version is currently multi-platform -
> even OSX, but not OS8 or 9 sadly).

No comment. Well, "Use a real OS", maybe? :-)
 
> btw - the code I am writing doesn't rely upon any platform specific
> stuff at all and is quite portable in Python - I will be aiming for the
> same thing in Python and assume it will be possible.

s/Python/C++/ ?

C++ is a dinosaur. If I were you, I'd spend time learning a
trendy language like Java (gag, spit, retch).

Cheers,

-- Joe
  "I'd rather chew my leg off than maintain Java code, which
   sucks, 'cause I have a lot of Java code to maintain and
   the leg surgery is starting to get expensive." - Me



More information about the Python-list mailing list