Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoking a method on an object

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")
like image 846
jmucchiello Avatar asked Sep 01 '09 19:09

jmucchiello


People also ask

What does it mean to invoke an object?

Invoke(Object, Object[]) Invokes the method or constructor represented by the current instance, using the specified parameters.

How do I invoke an object in Java?

You also use an object reference to invoke an object's method. You append the method's simple name to the object reference, with an intervening dot operator (.). Also, you provide, within enclosing parentheses, any arguments to the method. If the method does not require any arguments, use empty parentheses.

What does it mean to invoke a method?

To "invoke" appears to mean to call a method indirectly through an intermediary mechanism.

How can you invoke an object method in C#?

Invoke(Object, Object[]) has the following parameters. obj - The object on which to invoke the method or constructor. If a method is static, this argument is ignored. If a constructor is static, this argument must be null or an instance of the class that defines the constructor.


2 Answers

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.

like image 171
John Millikin Avatar answered Sep 17 '22 03:09

John Millikin


Your example would be:

PyObject* ret = PyObject_CallMethod(obj, "foo", "(s)", "An arg");
like image 42
Ned Batchelder Avatar answered Sep 21 '22 03:09

Ned Batchelder