3

我想知道如何在 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

这个怎么做 ?

谢谢

4

1 回答 1

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
于 2013-02-10T17:19:29.300 回答