Declaring self in PyObject_CallMethod

harold fellermann harold.fellermann at upf.edu
Fri May 13 06:08:49 EDT 2005


Hi,

> Calling a python method from C++ has the following signature:
>
> PyObject *
> PyObject_CallMethod(PyObject *self, char *method_name,
>                     char *arg_format, ...);
>
> I'm having trouble figuring out how to declare self.
>
> Let's say my python file is called stuff.py and is like the following,
> doMath() is defined in stuff.py and is not part of any class:
>
> #stuff.py
>
> def doMath():
>    val = val + 1
>
>
> In C++, I think my codes should be like the following:
>
> PyObject *resultObj = PyObject_CallMethod( self, "doMath", "");
>
> What do I put for self? Any help please?

it looks like you are confusing methods (bound to class objects) and
functions (bound to modules). if your doMath is a function defined in
a module, use

PyObject* PyObject_Call(PyObject *callable_object, PyObject *args, 
PyObject *kw)

i.e.

PyObject resultObj = PyObject_Call(doMath,NULL,NULL);

If, however, doMath is declared as a class-bound method, you have to use
PyObject_CallMethod() with a pointer to an instance of this class as the
first argument.

Cheers,

- harold -

--
Je me suis enfermé dans mon amour -- je rève.
-- Paul Eluard




More information about the Python-list mailing list