访问cffi枚举 [英] Accessing cffi enums

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

问题描述

假设我在cffi下定义了一个枚举:

Suppose I define an enum under cffi:

from cffi import FFI
ffi = FFI()
ffi.cdef('typedef enum {RANDOM, IMMEDIATE, SEARCH} strategy;')

现在,当再次调用cdef时,可以很容易地访问它.但是我该如何在不重新声明的情况下用python访问这个枚举呢?在文档中找不到任何提及.

Now this can be easily accessed when calling cdef again. But how would I then like to access this enum in python, without re-declaring it? Can't find any mentions in the docs.

推荐答案

使用ffi.dlopen,并通过使用ffi.dlopen的返回值进行限定来访问枚举值:

Use ffi.dlopen, and access the enum value by qualifying using the return value of the ffi.dlopen:

>>> from cffi import FFI
>>> ffi = FFI()
>>> ffi.cdef('typedef enum {RANDOM, IMMEDIATE, SEARCH} strategy;')
>>> c = ffi.dlopen('c')
>>> c.RANDOM
0
>>> c.IMMEDIATE
1
>>> c.SEARCH
2

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

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