Declaring self in PyObject_CallMethod

Fredrik Lundh fredrik at pythonware.com
Mon May 9 16:11:46 EDT 2005


"lamthierry at gmail.com" wrote:

> 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.

Reading the C API documentation might provide the clues you're
looking for:

    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.
    /.../ This is the equivalent of the Python expression "o.method(args)".

What is the object?  What method are you calling?  What arguments
are you passing in?

> 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

That's a function, not an object method.

> 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?

CallMethod is used to call a method on a given object.  To call a callable object
(such as a function), other callable object, use PyObject_Call (or CallObject
or CallFunction).   See the C API documentation for details.

</F>






More information about the Python-list mailing list