473,385 Members | 1,727 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,385 software developers and data experts.

Finding a python object in c++

I am trying to figure out how to get a reference to a python object in
c++ so that I can call methods from c++. For example if I have the
following python code

class foo:
def func():
x = 1

f1 = foo()
f1.func()

How do I find f1 at get a Py_Object* to it so I can call the func
method?

Thanks
Jul 18 '05 #1
3 1453
Access the 'im_self' attribute of the bound function object. In C,
I suppose this is something like
PyObject_GetAttrString(bound_m, 'im_self')
class F: ... def m(self): pass
... f = F()
bound_m = f.m
dir(bound_m) ['__call__', '__class__', '__cmp__', '__delattr__', '__doc__', '__get__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__repr__', '__setattr__', '__str__', 'im_class', 'im_func', 'im_self'] bound_m.im_self is f

1

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)

iD8DBQFBSjStJd01MZaTXX0RAthXAJ9KUU3K2THVK8UEa/NnQFxOINTm0ACgqMUa
sYDRj5571IPHVoaMO4r535Q=
=Oygg
-----END PGP SIGNATURE-----

Jul 18 '05 #2
Initially I was confused by your response because of my lack of
knowledge of how the Python interpreter works. I did a bit more
research and things are much more clear. My biggest problem was not
understanding how all defined objects are stored in the dictionary for
the imported module. Once I figured this out I was able to use the
following code to get f1 and call func.

//Assume python initialized and module imported
dict = PyModule_GetDict(module);
PyObject* pInst = PyDict_GetItemString(dict, "f1");
if(pInst) {
PyObject *pValue;
pValue = PyObject_CallMethod(pInst, "func", "");
if(pValue) {
Py_DECREF(pValue);
}
}

Note: this code doesn't handle error situations like making sure the
pInst variable is an instance object

Thanks,
BSMatt
On Thu, 16 Sep 2004 19:49:50 -0500, je****@unpythonic.net
<je****@unpythonic.net> wrote:
Access the 'im_self' attribute of the bound function object. In C,
I suppose this is something like
PyObject_GetAttrString(bound_m, 'im_self')
class F: ... def m(self): pass
... f = F()
bound_m = f.m
dir(bound_m) ['__call__', '__class__', '__cmp__', '__delattr__', '__doc__', '__get__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__repr__', '__setattr__', '__str__', 'im_class', 'im_func', 'im_self'] bound_m.im_self is f

1

Jul 18 '05 #3
Brian Matt <bs****@gmail.com> wrote:
...
//Assume python initialized and module imported
dict = PyModule_GetDict(module);
PyObject* pInst = PyDict_GetItemString(dict, "f1");
OK, but needlessly indirect. Doing just:

PyObject* pInst = PyObject_GetAttrString(module, "f1");

should, I think, be preferable.
if(pInst) {
PyObject *pValue;
pValue = PyObject_CallMethod(pInst, "func", "");
if(pValue) {
Py_DECREF(pValue);
}
}

Note: this code doesn't handle error situations like making sure the
pInst variable is an instance object


So much the better! If module.f1 has a func method that is callable
without arguments, why ever would you want to prohibit it from being
whatever type it wants to be? Maybe one day you'll want to recode the
whole type in C, and if you'd gone to the misplaced trouble of "making
sure [it] is an instance object" you'd just be creating headaches for
yourself...
Alex
Jul 18 '05 #4

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

Similar topics

5
by: davenet | last post by:
Hi, I'm new to Python and working on a school assignment. I have setup a dictionary where the keys point to an object. Each object has two member variables. I need to find the smallest value...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.