Pyrex - C function declaration in extension type

François Pinard pinard at
Mon Apr 21 04:05:52 CEST 2003

[Haris Bogdanovic]

> Pyrex doesn't let me define C function inside extension type (cdef class).
> Why is that and is there another way to acomplish the same thing (have C
> callable functions inside extension type) ?

C functions (Pyrex cdef's) are not directly callable from Python.  From
Python, one calls a Pyrex def, and from there, cdef's may be called.  So
from Python, only `def' within extension types may be used as methods.

Now, when I want a `cdef' associate with an extension type (call it
Shubbery, say!), instead of writing, _within_ Shubbery:

    cdef my_C_method(self, etc.

I write this _outside_ Shubbery:

cdef my_C_method(Shubbery self, etc.

That is, I use a typed `self' argument, and write the function body as if it
was a method from the extension type.  This is an acceptable compromise for
my needs, usually.  However, I have to call this function from within Pyrex
(it cannot be called directly from Python anyway) as a simple function,
passing it an explicit Shubbery as an actual argument for the formal `self',
avoiding the more comfortable `.' notation for methods.

On the other hand, I agree with you that it might be interesting being able
to declare `cdef's within extension types, for being later able to use the
`.'  notation for calling them, would it only be from within Pyrex.  But
this would be sugar, nothing fundamental nor essential so far that I see.

François Pinard

More information about the Python-list mailing list