【发布时间】:2021-12-14 09:16:24
【问题描述】:
我有 1 个 Cython .pxd 文件和 1 个 Cython .pyx 文件,pyx 文件包含一个 cdef 类:
# myclass.pyx (compiled to myclass.so)
cdef class myclass:
pass
现在是另一个特性的 .pxd 文件
# another.pxd (with another.pyx along)
from libcpp.vector cimport vector
import myclass # This line is funny, change 'myclass' to whatever and no syntax error
cdef vector[myclass] myvar # COMPILE TIME ERROR
cdef myclass myvar2 # SIMPLER, STILL COMPILE TIME ERROR
编译 another.pyx 时,Cython 显示关于 vector[myclass] 的错误,它说“myclass”是未知的。为什么会这样?
【问题讨论】:
-
你所做的永远不会奏效——你不能制作一个 cdef 类的向量。 cdef 类是一个 Python 对象,因此需要引用计数,而 vector 根本无法做到这一点。
-
@DavidW 好难过,所以 .pxd 文件中只允许使用 c/c++ 类型?
-
没有。您可以在 pxd 文件中使用 cdef 类。您不能将它们存储在 C++ 向量中。
-
.pxd 文件只能包含编译时的东西。因此它需要是
cimport myclass。这也意味着应该有一个myclass.pxd(因为cimport寻找一个pxd 文件,而不是一个pyx 文件) -
那是因为
import myclass发生在运行时。 .pxd 文件应该包含编译时cimport myclass(或者可能是cimport myclass from myclass)
标签: python c++ import compiler-errors cython