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

Stefan Behnel stefan_ml at behnel.de
Thu Apr 23 02:01:25 EDT 2009


Hi,

dug.armadale at googlemail.com wrote:
> 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.
> 
> 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.

My advice is to

1) write the program in Python, in a way that makes it well comprehensible
and maintainable

2) take care to use suitable algorithms and builtin types

3) benchmark important parts of the code early, fix your algorithms

4) benchmark/profile the code when you think it's ready, optimise it where
required

If you find places where you require plain performance that you think
cannot be achieved in Python,

5) extract those functions or classes into one or more separate modules,
compile them with Cython and import them into your normal code. Try to also
extract surrounding code sections (especially loops) to avoid Python's call
overhead.

6) add type declarations until it runs fast enough (usually a couple of
hundred times faster for I/O free code)

You may be able to compile whole files in Cython (so that you can skip the
refactoring required for step 5), but that is neither required nor
necessarily the best way to do it, since binary modules are always harder
to handle/fix/understand than plain Python modules. It will also keep you
from adding type declarations in places where it might hurt readabilty more
than it brings performance.


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

My advice regarding this paragraph is to not take advices like this. Ever.

Stefan



More information about the Python-list mailing list