创建使用Python的C API的对象 [英] Create an object using Python's C API

查看:202
本文介绍了创建使用Python的C API的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说,我有我的定义对象布局为:

Say I have my object layout defined as:

typedef struct {
    PyObject_HEAD
    // Other stuff...
} pyfoo;

...和我的类型定义:

...and my type definition:

static PyTypeObject pyfoo_T = {
    PyObject_HEAD_INIT(NULL)
    // ...

    pyfoo_new,
};

如何创建新实例 pyfoo 中我的C扩展的地方?

推荐答案

<击>呼叫 PyObject_New() ,其次是 PyObject_Init()

Call PyObject_New(), followed by PyObject_Init().

编辑:最好的办法就是到调用类对象,就像在Python本身:

The best way is to call the class object, just like in Python itself:

/* Pass two arguments, a string and an int. */
PyObject *argList = Py_BuildValue("si", "hello", 42);

/* Call the class object. */
PyObject *obj = PyObject_CallObject((PyObject *) &pyfoo_T, argList);

/* Release the argument list. */
Py_DECREF(argList);

这篇关于创建使用Python的C API的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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