Finding a python object in c++

Brian Matt bsmatt at gmail.com
Fri Sep 17 16:12:14 CEST 2004


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, jepler at unpythonic.net
<jepler at 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
> 
> 
> 
>



More information about the Python-list mailing list