[Cython] make extension type accesible from python?

Neal Becker ndbecker2 at gmail.com
Mon Mar 3 13:54:50 CET 2008


I'm trying to learn cython.  As a simple first try, I have some generic c++
code 'my_sum'.  I want to pass it an intvec (std::vector<int>).

It seems to work OK when accessed from c-code:

cdef extern from "vector":
    pass

cdef extern from "test1.hpp":
    ctypedef struct intvec "std::vector<unsigned int>":
        void (* push_back)(int elem)

    intvec intvec_factory "std::vector<unsigned int>"(int len)

    int my_sum "my_sum<std::vector<unsigned int> >"(intvec vec)



cdef intvec v = intvec_factory(2)
v.push_back(2)

print my_sum (v)

I have no idea how to expose intvec to python, and then be able to call
my_sum from python.

I assume there is some way to make extension types that can be used from
python?  Of course, I'd want to be able to add other methods, so that, for
example, intvec can be constructed and manipulated from python.

Any simple examples I could look at?



More information about the Cython-dev mailing list