Python C API中的静态变量 [英] Static Variables in Python C API
本文介绍了Python C API中的静态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何暴露这样的静态"变量
How would one expose "static" variables like this
class MyClass:
X = 1
Y = 2
通过C API? PyTypeObject上唯一看起来正常工作的变量是tp_members,但我看不到PyMemberDef中的标志来指示该成员应按类而不是按实例.
via the C API? The only variable on the PyTypeObject that looks like it would work is tp_members, but I see no flag in the PyMemberDef to indicate that the member should be per-class, not per-instance.
需要更多说明,因为它可能会改变答案,所以我试图向Python公开C枚举,以便枚举
For a bit more clarification, since it may change the answer, I'm trying to expose a C enum to Python such that the enumeration
enum MyFlags {
Alpha = 0,
Beta = 1
};
可以在Python中以以下方式访问:
Can be accessed in Python as:
module.MyFlags.Alpha
module.MyFlags.Beta
推荐答案
只需将它们放入类型的 tp_dict 例如与 PyDict_SetItemString .
Just put them in the type's tp_dict e.g. with PyDict_SetItemString.
这篇关于Python C API中的静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文