在Cython中创建对象列表

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

如何做到这一点?谢谢。
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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接