Extending Python the right way...

Pearu Peterson pearu at cens.ioc.ee
Fri Dec 13 04:25:54 EST 2002


On 13 Dec 2002, JB wrote:

> I'd like to have a Python class
> class SL ...
> that represents for example a straight line. So it has 3 
> coordimates, a b and c. But a, b and c mut be multi 
> precision numbers and I cannot use Python for this. So I 
> should like to associate to each instance of my Sl class 
> three C++ objects (multi precision numbers, provided by an 
> external C++ library. I thought, I could simply right a 
> Python extension for this, but it is not clear to me, if 
> this would really be the right solution.
> 
> For example having three classes
> class POINT #represents point
> class SL #straight line
> 
> g = SL(1,2,5)
> h = SL(3,-1,0)
> p = section(g,h)
> 
> where p is an instance of the class point. The section would 
> be computed inside C++, that is the coordinates of the 
> resulting point would be computed in C++ and they should 
> again be associated with the instance of the class POINT.
> 
> If I use Python floats for the coordinates, things are very 
> simple as I can stay within Python. (I have alread written 
> a working prototype.)
> 
> But it would be very nice if I could use the multi precision 
> arithmetic offered by an external C++ library.
> 
> Could anybody give me a hint, how to proceed?

There are several Python interfaces to GMP (Gnu Multi-Precision) library,
see

  http://gmpy.sourceforge.net/
  http://www.egenix.com/files/python/mxNumber.html

so there is no immediate need to use C++ extensions for your problem.

Or if you want to wrap external C++ libraries to Python, I suggest
Boost.Python

  http://www.boost.org/libs/python/doc/index.html

Pearu





More information about the Python-list mailing list