我想做相同的
class Foo(object):
bar = 1
使用Python的C API。换句话说,我想使用C。
创建一个具有静态变量的Python类我该怎么做?
答案 0 :(得分:5)
PyTypeObject type;
// ...other initialisation...
type.tp_dict = PyDict_New();
PyDict_SetItemString(type.tp_dict, "bar", PyInt_FromLong(1));
答案 1 :(得分:2)
您可以使用适当的标记将该源代码传递给Py_CompileString。
如果您已经上课,可以使用PyObject_SetAttr。