From 269963f8a3b86f96bf0d5543f2d55da111d08e5d Mon Sep 17 00:00:00 2001 From: nbruin Date: Wed, 10 Apr 2013 19:32:56 -0700 Subject: [PATCH] explain difference between cdef and cpdef --- docs/src/tutorial/cdef_classes.rst | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/docs/src/tutorial/cdef_classes.rst b/docs/src/tutorial/cdef_classes.rst index 982930b..2791735 100644 --- a/docs/src/tutorial/cdef_classes.rst +++ b/docs/src/tutorial/cdef_classes.rst @@ -32,20 +32,25 @@ Cython code and pure Python code. So far our integration example has not been very useful as it only integrates a single hard-coded function. In order to remedy this, -without sacrificing speed, we will use a cdef class to represent a +with hardly sacrificing speed, we will use a cdef class to represent a function on floating point numbers:: cdef class Function: cpdef double evaluate(self, double x) except *: return 0 -Like before, cpdef makes two versions of the method available; one +The directive cpdef makes two versions of the method available; one fast for use from Cython and one slower for use from Python. Then:: cdef class SinOfSquareFunction(Function): cpdef double evaluate(self, double x) except *: return sin(x**2) +This does slightly more than providing a python wrapper for a cdef +method: unlike a cdef method, a cpdef method is fully overrideable by +subclasses and instance attributes. This adds a little calling overhead +compared to a cdef method. + Using this, we can now change our integration example:: def integrate(Function f, double a, double b, int N): @@ -62,8 +67,8 @@ Using this, we can now change our integration example:: print(integrate(SinOfSquareFunction(), 0, 1, 10000)) This is almost as fast as the previous code, however it is much more flexible -as the function to integrate can be changed. It is even possible to pass -in a new function defined in Python-space:: +as the function to integrate can be changed. We can even pass in a new +function defined in Python-space:: >>> import integrate >>> class MyPolynomial(integrate.Function): -- 2.7.4