Programming in Python with a view to extending in C at a later date.

Terry Reedy tjreedy at
Tue Apr 21 00:32:06 CEST 2009

dug.armadale at wrote:
> Hi,
> Say you set out to program in Python knowing that you will be
> converting parts of it into C ( or maybe C++) at a later date, but you
> do not know which parts.

I presume because you intend to wait for profile results.  Good.
Of course, you might even find your program *fast enough*.

> Can you give any general Python structure / syntax advice that if
> implemented from the start, will make this future task less painful.
> In addition, advice that will make this easier for automatic tools
> will be good too.

I would hate for you to make your Python experience more painful by 
overly restricting yourself.  A good test suite (in Python!) will make 
any translation less painful.  This goes also for trying out different 
algorithms in Python, or otherwise refactoring.  You also might be able 
to get the speedup you need by compiling with PyRex, Cython, Shedskin, 
Weave, or Pscho, or by using existing C code that you find when you need it.

> This advice might include stuff like "avoid using Python yield
> as ...."

I am not sure which, if any, of the above handle generators directly, 
but generators often make Python programming easier, and there are 
several other solutions.
0) Replace the body that calculates what is yielded with a C-coded 
function, and leave the generator alone.
1) If you are explicitly calling next(somegen), rewrite the generator in 
C as a next function.
2) Rewrite the generator as an iterator and translate that.


More information about the Python-list mailing list