On Jun 3, 2008, at 5:38 AM, Jan Spurný wrote:
> Hi,
>
> I just found cython and I'm trying to speedup my programs with
> it... I have several classes which are bottlenecks of my
> applications and I want to make them as fast as possible while
> keeping the same interface as old python classes had..
Sounds like a perfect fit for Cython.
> I would like to use cdef-ed methods in python
> so I'm trying to do something like this:
>
> a.pyx:
> ----------------------------------------------------
> cdef class A:
> cdef public void a(A self):
> print 'method A::a'
> return
> ----------------------------------------------------
>
> pa.py
> ----------------------------------------------------
> from a import A
>
> xa = A()
> xa.a()
> ----------------------------------------------------
>
> and it says:
> ----------------------------------------------------
> python pa.py
> Traceback (most recent call last):
> File "pa.py", line 4, in <module>
> xa.a()
> AttributeError: 'a.A' object has no attribute 'a'
>
> ----------------------------------------------------
>
> thanks for any suggestions..
The problem here is that cdef methods cannot be called from Python--
they are literally C functions only visible from C. You need either
normal "def" functions (which still get compiled to fast C) or
"cpdef" functions that are a hybred of cdef and def. What you
probably want to do is
cdef class A:
cpdef a(self):
# do stuff
which will compile the method a() into fast c code but still be
accessible from Python, and if you call it from Cython then it avoids
Python calling conventions. Note that "self" is automatically of type
A, and the public keyword is something completely separate (used for
interfacing with outside C code).
- Robert
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev