[SciPy-dev] Cython / python policy

Sturla Molden sturla@molden...
Fri Mar 6 19:55:09 CST 2009


Matthew Brett wrote:
> I was just thinking of doing some Cython.
> Do we think that, in general, scipy code should have both C(ython)
> _and_ python implementations of the same thing, with different names,
> as for Anne's spatial package?y-dev
>   
I think SciPy should adopt the same policy as Python 3. That is, first 
import a Python module, then try to import a C module on top of that, 
replacing names.

Having two versions with similar functionality but different name in 
Python and C (e.g. pickle and cPickle, profile and cProfile, KDTree and 
cKDTree) is not a good idea. SciPy will move to Python 3 at some point 
anyway, and doing this differently from other Python packages would be 
confusing. It is less messy this way as well, as we avoid subtle 
differences between the C and Python objects - any difference would be 
considered a bug. It would also mean that anything is prototyped in 
Python first, before migration to C. Hopefully it would also limit the 
use of C to the parts that really benefit from migration, as the working 
Python code is always written first.

To use Anne's kd-tree as an example, a tentative organization of the 
code would be a Python module kdtree.py with a full Python 
implementation of teh kd-tree, and a Cython file ckdtree.pyx with a 
partial Cython re-implementation.

In kdtree.py:

   class baseKDTree(object):
      # full python implementation here

   try:
      from ckdtree import KDTree 
   except ImportError:
      class KDTree(baseKDTree):
          pass

   __all__ = ['KDTree']


In ckdtree.pyx:

   from kdtree import baseKDTree
    
   class KDTree(baseKDTree):
      # Cython re-implementation of the slowest
      # parts of the base class, the rest we keep
      # in Python.



Sturla Molden










More information about the Scipy-dev mailing list