[Pyrex] [Cython-dev] C versus Python access

Robert Bradshaw robertwb at math.washington.edu
Mon Dec 31 18:14:57 CET 2007


On Oct 30, 2007, at 7:22 PM, David McNab wrote:

> On Tue, 2007-10-30 at 15:45 -0700, Robert Bradshaw wrote:
>> Funny you should ask, there's a new call type in Cython that does
>> exactly this.
> 8><
>>      cpdef int area(self):
>>          return self.width * self.height
>>
>> It will create a (python-accessible) def function that wraps the
>> cpdef function, but if the call is made on a Rect object, e.g.
>>
>> cdef Rect my_rect = Rect(3,4)
>> print my_rect.area()
>>
>> then the fast c method is called. The result is much cleaner code and
>> no messy wrapper functions with different names. On top of that it is
>> still overrideable, if you have a cdef subclass, override it with a
>> cpdef method, and if you (or someone using your code) creates a
>> python subclass, they can override it with a normal python method
>> (which won't be as fast of course, but will get called even if the
>> call is made from pyrex). The overhead is extremely small. This is an
>> extremely new functionality and needs to be better documented. (It
>> used to be called rdef by the way.)
>
> Hi Robert,
>
> Thanks for switching me on. I'm trying out Cython - so far, so good. I
> could end up defecting for good.
>
> Is there an up-to-date list of the Cython/Pyrex differences  
> anywhere? I
> looked on the Cython website and wiki, but no such list jumped out at
> me.
>
> Are there any other goodies apart from cpdef?

See http://wiki.cython.org/DifferencesFromPyrex , linked from the  
main wiki, which we try to keep up to date.

- Robert



More information about the Pyrex mailing list