example sources in both Python and C++

Bill McClain wmcclain at salamander.com
Mon Oct 2 19:27:53 EDT 2000


Here is something for the beginning Python programmer, or
anyone who wants to compare the language with C++.

I have put up an astronomical package implemented in both
Python and C++. It is just a subroutine library and a few
simple applications, all with console interfaces:

    http://astrolabe.sourceforge.net/
    
This is probably not the best example for language comparison,
in that most of the code is numerical and would look much
the same in Fortran. But comparing cronus.py with cronus.cpp
gives some idea of how tasks are approached in both languages.

Some notes from the home page:

  Years ago, I originally wrote the astrolabe routines in C. Now, I
  always implement in Python first, then make a C++ version as a
  more-or-less direct translation. The C++ source has a lot of
  compromises required for compatability with different compilers,
  which are, however, getting closer to the standard with time.

  The Python sources are obviously more compact than the C++
  versions and have less declarative overhead. To me, the most
  unfortunate aspects of C++ are:

     * STL containers cannot have initialization lists, meaning that
       inline data must first be held in C-arrays, then copied to
       the containers at run-time. I use some macros to make this a
       bit more natural-looking.
     * Template arguments cannot be local types. That means STL
       containers must use types declared at the outer scope, which
       is ugly.
     * Functions may not return multiple values. Where Python can
       have:

            ra, dec = func()

       C++ requires:

            void func(double &ra, double &dec);
            double ra, dec;
            func(ra, dec);

       ...or a struct for a return value, which would require more
       declarations. In an alternative C-like language, this would
       be just as strongly-typed as the function shown above:

            double, double func();
            double ra, double dec = func();

-Bill



More information about the Python-list mailing list