定义将在C部分代码中使用的Cython代码中的枚举 [英] Defining enums in Cython code that will be used in the C part of code

查看:130
本文介绍了定义将在C部分代码中使用的Cython代码中的枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在cython头文件中定义了枚举 api.pxd

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

我还检查了转动 ctypedef cdef 会工作(没有)。

I also have checked if turning ctypedef to cdef would work (and it didn't).

我想在 __ cinit __ 方法中使用这个枚举的值,一些类:

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

我收到编译错误:

    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?

推荐答案

您不能通过其属于的类型名称访问枚举常量,也不在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.

这篇关于定义将在C部分代码中使用的Cython代码中的枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆