调用对象的方法

发布于 2024-08-03 08:52:37 字数 368 浏览 2 评论 0原文

给定一个 PyObject* 指向一个 python 对象,如何调用其中一个对象方法?该文档从未给出这样的示例:

PyObject* obj = ....
PyObject* args = Py_BuildValue("(s)", "An arg");
PyObject* method = PyWHATGOESHERE(obj, "foo");
PyObject* ret = PyWHATGOESHERE(obj, method, args);
if (!ret) {
   // check error...
}

这相当于

>>> ret = obj.foo("An arg")

Given a PyObject* pointing to a python object, how do I invoke one of the object methods? The documentation never gives an example of this:

PyObject* obj = ....
PyObject* args = Py_BuildValue("(s)", "An arg");
PyObject* method = PyWHATGOESHERE(obj, "foo");
PyObject* ret = PyWHATGOESHERE(obj, method, args);
if (!ret) {
   // check error...
}

This would be the equivalent of

>>> ret = obj.foo("An arg")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昔日梦未散 2024-08-10 08:52:37
PyObject* obj = ....
PyObject *ret = PyObject_CallMethod(obj, "foo", "(s)", "An arg");
if (!ret) {
   // check error...
}

阅读 Python C API 文档。在本例中,您需要对象协议

PyObject* PyObject_CallMethod(PyObject *o, char *method, char *format, ...)

返回值:新引用。

以可变数量C调用对象o的名为method的方法
论据。 C 参数是
通过 Py_BuildValue() 格式描述
应该生成元组的字符串。
格式可能为NULL,表示
没有提供任何论点。
返回调用结果
成功,或失败时NULL。这是
相当于Python
表达式o.method(args)。注意
如果您只传递 PyObject * args
PyObject_CallMethodObjArgs() 是一个
更快的替代方案。

PyObject* PyObject_CallMethodObjArgs(PyObject *o, PyObject *name, ..., NULL)

返回值:新引用。

调用对象o的方法,其中给出了方法的名称
作为名称中的 Python 字符串对象。它
被调用的数量可变
PyObject* 参数。论据是
提供为可变数量
参数后跟NULL。退货
成功调用的结果,或
失败时NULL

PyObject* obj = ....
PyObject *ret = PyObject_CallMethod(obj, "foo", "(s)", "An arg");
if (!ret) {
   // check error...
}

Read up on the Python C API documentation. In this case, you want the object protocol.

PyObject* PyObject_CallMethod(PyObject *o, char *method, char *format, ...)

Return value: New reference.

Call the method named method of object o with a variable number of C
arguments. The C arguments are
described by a Py_BuildValue() format
string that should produce a tuple.
The format may be NULL, indicating
that no arguments are provided.
Returns the result of the call on
success, or NULL on failure. This is
the equivalent of the Python
expression o.method(args). Note that
if you only pass PyObject * args,
PyObject_CallMethodObjArgs() is a
faster alternative.

And

PyObject* PyObject_CallMethodObjArgs(PyObject *o, PyObject *name, ..., NULL)

Return value: New reference.

Calls a method of the object o, where the name of the method is given
as a Python string object in name. It
is called with a variable number of
PyObject* arguments. The arguments are
provided as a variable number of
parameters followed by NULL. Returns
the result of the call on success, or
NULL on failure.

Smile简单爱 2024-08-10 08:52:37

你的例子是:

PyObject* ret = PyObject_CallMethod(obj, "foo", "(s)", "An arg");

Your example would be:

PyObject* ret = PyObject_CallMethod(obj, "foo", "(s)", "An arg");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文