[C++-sig] c + python interchangable?

Jason Kankiewicz jkankiewicz at advpubtech.com
Wed Jul 6 18:33:01 CEST 2005


danny280279 at netscape.net wrote:
> So what your saying is (code here:)
> 
> #define DEBUG
> 
> #include 
> #include 
> #include 
> 
> are static links to code comapred to 
> 
> def debug
> 
> import
> import
> import
> 
> because to me they seem the same, or am i just being stupid?
There's a huge difference between the two. In C, all external linkage 
(except for shared libraries) is established at the time the program is 
compiled into an executable binary form. In Python, all external linkage 
is established at the time that the program is run.

The "#include" directive is, in fact, causing the preprocessor to 
substitute it with the contents of the entire file that it's specifying. 
The contents of header files are typically contained within a
#ifndef A_UNIQUE_LABEL\n#define A_UNIQUE_LABEL\n...#endif\n block in 
order to prevent them from being inserted multiple times because of 
redundant "#include" directives. Your C program would have to be 
re-compiled for any subsequent changes to an included header to have any 
effect on it because a compiled program, except for any shared libraries 
  that it links to, is completely self-contained.

Python's "import" statement, on the other hand, dynamically loads a 
module which must be present in a directory specified within Python's 
path environment variable. If an imported module is modified and then 
re-imported its changes will have an effect on your program. Your Python 
program is not self-contained; any scripts and/or extensions that it 
imports must be distributed along with it.

> if that's the case then are there any docs on hybrid programming, because then i could implement all of it in hybrid python/c.
I see hybrid programming as an iterative process.
1) Write your entire program in Python because you'll get a working 
product together faster than if you wrote it in C or C++ both in terms 
of initial implementation and debugging.
2) If the program is too slow, use Python's profiling tools to identify 
the part of your program that is running the slowest.
3) Move the slowest function or method into its own module and add an 
"import" statement to the module where it used to reside so that it will 
still be visible there. If it's a method it'll have to be declared as a 
standalone function in the external module so also add an assignment 
statement to its former class' implementation module that binds it to 
the class as a method.
4) Here comes the hybrid part: re-implement the "hot spot" module as a 
Python extension. Because it will be compiled directly to machine code 
instead of being interpreted, the re-implementated code should run a lot 
faster than the original.
5) Repeat steps 2-4 until the program is fast enough.

I recommend that you use C++ instead of C because it is more similar to 
Python and it will therefore be easier for you to translate Python code 
into C++ than it would be for you to translate it into C. C++ has 
std::vector which approximates Python's list, std::map which 
approximates Python's dictionary, std::string which approximates 
Python's string, etc. C just has the array. Python's extension API is 
for C only but the Boost.Python library, which this very mailing list is 
all about, allows you to use C++ for writing Python extensions instead.
It's a fantastic piece of work.

> I know that this is going to take up a bit of space and time but I mean if I can implement this easier the way I want to then so much the better.  It would be easier for someone like yourself to use too, if you were to type "wget http://url/package" "gunzip package" "cd dir" "./configure" "make" "make clean" "make install" "package", compared to "search package install run" using natural language commands that you expect to be there.  That's the hope anyway, but as you can see this just finds files on your local system, and doesn't do any of the stuff that I wanted it to, I did mainly lift it from gnu find but i've got another load of dirs under the same command dir 1 is for wget, 1 is for findfiles (the file I sent you), 1 is for nlp code so that find/search are accepted as the actual commands in bash instead of all of the above, etc etc so now I'm going to do it right and send that to 
>  gnu.  Any docs you can point me in the direction of?  
For an introduction to hybrid programming with Boost.Python read this 
article http://www.boost-consulting.com/writing/bpl.html.




More information about the Cplusplus-sig mailing list