Declaring self in PyObject_CallMethod

L

lamthierry

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

Fredrik Lundh

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

harold fellermann

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 -
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,239
Messages
2,571,199
Members
47,835
Latest member
CyrusRuggi

Latest Threads

Top