在Cython中创建一个对象列表

时间:2013-02-10 15:52:02

标签: python list object cython

我想知道如何在Cython中创建C对象列表。

这个简单的例子起作用:

cimport cython

b = real_test()
print(b)

cdef real_test():
    cdef int a
    cdef Node b = Node()
    a = b.h
    return a

cdef class Node:
    cdef int h
    def __cinit__(self):
        self.h = 3

但不是这样:

cimport cython

b = real_test()
print(b)

cdef real_test():
    cdef int a
    cdef Node *b = [Node(),Node(),Node()]
    a = b[0].h
    return a

cdef class Node:
    cdef int h
    def __cinit__(self):
        self.h = 3

怎么做?

感谢

1 个答案:

答案 0 :(得分:1)

我不确定是对的,但是它有效:

cimport cython

b = real_test()
print(b)

cdef real_test():
    cdef int a
    cdef list b = [Node(),Node(),Node()]
    a = b[0].h
    return a

cdef class Node:
    cdef int h
    def __cinit__(self):
        self.h = 3
    property h:
        def __get__(self):
          return self.h
        def __set__(self, float value):
          self.h = value