在 Cython 代码中定义将在代码的 C 部分中使用的枚举
我已经在 cython 头文件 api.pxd
中定义了 enum
:
ctypedef enum InstructionType:
default = 0
end_if = 1
end_loop = 2
backward_jump_here = 4
我还检查了是否将 ctypedef
转换为 cdef
会起作用(但没有)。
我想在某个类的 __cinit__ 方法中使用此枚举的值:
from api cimport Instruction, CLinVM, InstructionType
# (...) some other classes
cdef class EndIf(Noop):
def __cinit__(self):
self.type = InstructionType.end_if
我收到编译错误:
self.type = InstructionType.end_if
^
------------------------------------------------------------
/home/(...)/instructions.pyx:149:35: 'InstructionType' is not a constant,
有什么方法可以以这种方式定义和使用枚举吗?
I have defined and enum
in cython header file api.pxd
:
ctypedef enum InstructionType:
default = 0
end_if = 1
end_loop = 2
backward_jump_here = 4
I also have checked if turning ctypedef
to cdef
would work (and it didn't).
And I want to use value from this enum in __cinit__
method fo some class:
from api cimport Instruction, CLinVM, InstructionType
# (...) some other classes
cdef class EndIf(Noop):
def __cinit__(self):
self.type = InstructionType.end_if
And I get compilation error:
self.type = InstructionType.end_if
^
------------------------------------------------------------
/home/(...)/instructions.pyx:149:35: 'InstructionType' is not a constant,
Any way to define and use enum in such way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无论是在 C、C++ 还是 Cython 中,您都不能通过其所属的类型名来访问枚举常量。您需要为其创建一个包装器.pxd。
You do not access enumerated constants through their typename they belong to, neither in C, nor in C++, nor in Cython. You'd need to create a wrapper .pxd for it.