Using PyObject_CallObject on an Instantiated Class

John Smith jsmith at thebigwave.net
Tue Aug 17 05:53:50 CEST 2004


Greetings All,

I have something similiar to this:

Foo.py
class Foo(object):
    def function(self):
        pass

Bar.py
class Bar(Foo):
    def function(self):
        self.variable = self.variable + 1

Bleen.py
def GetBar():
    package = __import__('Directory.Bar')
    module = getattr(package, 'Bar')
    classobj = getattr(module, 'Bar')
    return classobj()

>From C++, I call
    PyObject* pBar = PyObject_CallMethod(pBleen, "GetBar"); // Not
exactly but close enough

Now, I'd like to save off some pointers to this instance of
Bar.function so that I can call it without the lookup overhead of the
PyObject_CallMethod (IE: using PyObject_CallObject)

However, Anything that I try to do fails.
>From python, foo.__dict__ is empty, and foo.__class__.__dict__ refers
to the base class obviously.  If I do the equivilant from C++ of:

PyObject* sClass = PyString_FromString("__class__");
PyObject* pClass = PyObject_GetAttr(m_pObject, sClass);
PyObject* sDict = PyString_FromString("__dict__");
m_pDictionary = PyObject_GetAttr(pClass, sDict);

Then m_pDictionary shows as empty in PyDict_Next() (Probably because
it is dictproxy)

So given that I'd have a few thousand Bars() each in it's own C++
class that would like to have a PyObject* to function() that would
contain it's own 'self.variable' how would I get the pointer to it so
that I could call PyObject_CallObject on it?

Thanks!
-John



More information about the Python-list mailing list