I have following test.pyx
cdef public class Foo[object Foo, type FooType]:
cdef public char* foo(self):
r = "Foo"
return r
cython compiles that code to test.h and test.c and everything looks fine, but I can't figure out how to create Foo object from C-code.
Even if I create it using Cython function:
cdef public Foo create_Foo():
return Foo()
I can't figure out how to invoke foo method.
Thanks.
Cython improves the use of C-based third-party number-crunching libraries like NumPy. Because Cython code compiles to C, it can interact with those libraries directly, and take Python's bottlenecks out of the loop.
Since Cython is based on C runtime, it allows you to use cdef and cpdef . cdef declares function in the layer of C language. As you know (or not?) in C language you have to define type of returning value for each function. Sometimes function returns with void , and this is equal for just return in Python.
Overview. Cython has native support for most of the C++ language. Specifically: C++ objects can be dynamically allocated with new and del keywords.
Cython is fast at the same time provides flexibility of being object-oriented, functional, and dynamic programming language. One of the key aspects of Cython include optional static type declarations which comes out of the box.
That answer I've got in Cython User Group:
Public should probably be disallowed for methods, since calling the functions directly would break subclassing. If you need to invoke the method from C, write a wrapper function for each method that takes the object as extra argument, and invoke the method. E.g.
cdef char *foo_wrapper(Foo obj):
return obj.foo()
It's inconvenient if you have many methods, so if you have control over the design of Foo to begin with, don't use methods but use functions instead.
Specify create_ foo
as def
or cpdef
. cdef
- ed functions are converted entirely in C Code and will not be exposed to the Python module.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With