473,387 Members | 1,365 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Declaring self in PyObject_CallMethod

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?

Jul 19 '05 #1
2 4590
"la********@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>

Jul 19 '05 #2
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

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Carlos Ribeiro | last post by:
Hello all, I'm posting this to the list with the intention to form a group of people interested in this type of solution. I'm not going to spam the list with it, unless for occasional and...
5
by: fred | last post by:
I don't know if I'm doing this correctly. I have a little programming experience in python, c++ and some others but this is my first time with javascript. I'm trying have my website detect the...
2
by: MLH | last post by:
I have a form module that starts out something like this... Option Compare Database 'Use database order for string comparisons ..... Then there's a line like this in it... Public Const...
6
by: Steve Jorgensen | last post by:
Many of the regulars here have explained that declaring variables using As New .... is a bad idea, and some have given some good explanations, but I wanted add one more demonstration to the mix. ...
16
by: G Patel | last post by:
Hi, If I want to call functions that don't return int without declaring them, will there be any harm? I only want to assign the function(return value) to the type that it returns, so I don't...
11
by: arekkusu | last post by:
Hello, I have the following problem: when declaring hex data in C, you typically do something like: const char = {0xde, 0xad, 0xbe, 0xef, 0x01, 0x02, 0x03, 0x04 ... }; This is quite verbose...
1
by: Maarten Terlingen | last post by:
Sample: ClassA: ClassB ClassB: ClassC ClassA x = new Class C I want to know the declaring type of C. x.GetType() returns C x.GetType().BaseType returns B x.GetType().BaseType.BaseType...
2
by: Huayang Xia | last post by:
I am trying to use PyObject_CallMethod. It needs a format string to specify what are the followed arguments. Is it possible to use a PyObject* as an argument? Where can I find the spec for the...
9
by: =?ISO-8859-1?Q?Janne_H=E4rk=F6nen?= | last post by:
Hello, Is there a simple way to resolve declaring class of a method at runtime ? Consider this simple example: $ python Python 2.5.1 (r251:54863, May 18 2007, 16:56:43) on cygwin Type...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.