包装cython的枚举类 [英] Wrap enum class for cython

查看:79
本文介绍了包装cython的枚举类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将枚举类包装在c ++头文件中以用于cython项目。我已经在Google周围搜索了一下,但找不到实现该方法的方法-是否受支持?

I am trying to wrap an enum class in a c++ header file for use in a cython project.I have googled around and can not find out how to achieve this - is it supported?

推荐答案

CPP类



CPP class

enum class Color {red, green = 20, blue}; 



类型的定义



Definition of type

cdef extern from "colors.h":
  cdef cppclass Color:
    pass



颜色类型的定义



Definition of color types

cdef extern from "colors.h" namespace "Color":
  cdef Color red
  cdef Color green
  cdef Color blue



Python实现



Python implementation

cdef class PyColor:
  cdef Color thisobj
  def __cinit__(self, int val):
    self.thisobj = <Color> val

  def get_color_type(self):
    cdef c = {<int>red : "red", <int> green : "green", <int> blue : "blue"}
    return c[<int>self.thisobj]

这篇关于包装cython的枚举类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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