Re: Declaring self in PyObject_CallMethod

2005-05-13 Thread 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 -

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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Declaring self in PyObject_CallMethod

2005-05-09 Thread Fredrik Lundh
[EMAIL PROTECTED] 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



-- 
http://mail.python.org/mailman/listinfo/python-list