python - 如何在 cython 中访问枚举?

标签 python c++ enums cython

<分区>

我有一个很大的 c++ 代码,我将其包装在 cython 中以从 python 调用。我不确定在 cython 中应该如何调用枚举中的内容。这是我已经尝试过的。但这还不完整。帮助!

演示示例

class Foo{
public:
   Foo1 bar(enum_is type = enum::any) const;
}
enum class enum_is:unsigned int{
 any = 0,
 one = 1,
 two = 2,
}; 

abc.pxd

cdef extern from "headerfile.hpp":
    cdef cpp class enum_is:
        pass

cdef extern from "headerfile.hpp" namespace "enum_is":
    cdef enum_is any
    cdef enum_is one
    cdef enum_is two

abc.pyx

cdef class Pyenum_is:
    cdef enum_is thisobj
            def __cinit__(self,unsigned int val):
                self.pin_isthisobj = <pin_is> val
            def get_pin_is_type(self)
                cdef r={<unsigned int> any:"any",
                        <unsigned int> one:"one",
                        <unsigned int> two:"two"
                       }
                return r[<unsigned int>self.thisobj]

假设我正确包装了枚举类,我需要有关如何在 python 中实际使用枚举的函数的帮助

cdef class PyFoo1
    cdef Foo1 Foo1thisobj
    cdef Foo1* Foo1thisptr

cdef class PyFoo
    cdef Foo Foothisobj
    cdef Foo* Foothisptr
    def bar(self,#pass enum_is object here):
        cdef Foo tempv = self.thisobj.bar(#pass enum here)
        abc = PyFoo()
        abc.Foo1thisobj = tempv
        return abc

有人可以帮助我了解如何继续在 cython 中使用这个枚举

最佳答案

你可以像在 Cython 中声明一个枚举

ctypedef 枚举选项:OPT1、OPT2、OPT3

然后举个例子:

def main():
    cdef options test
    test = OPT2
    f(test)

cdef void f(options inp):
    if inp == OPT1:
        print('OPT1')
    elif inp == OPT2:
        print('OPT2')
    elif inp == OPT3:
        print('OPT3')

关于python - 如何在 cython 中访问枚举?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46838415/

相关文章:

c++ - 构建静态库 (.lib) VS 2010 Pro

c++ - 在无序映射 <string, int> 中存储大数字(10-12 位)

c# - 如何对空枚举进行单元测试?

python - pty多路复用器

python - debian box 上的全局应用样式表链接

python - 如何从分组数据创建直方图

c++ - func() + func() 是未定义的行为吗?

python - 有什么方法可以正确打印 OrderedDict?

mysql - MySQL/JDBC 中的动态 ENUM

c# - 从属性创建枚举