Accessing members of Python class from C++ (Embedded Python)????

JM jgerard at fluid-studios.ca
Wed Aug 27 13:14:02 EDT 2003


If anyone is interested in the solution:

pDict=PyObject_GetAttrString(PyObject,"__dict__") will get the local
symbol table (a dictionary) for the class.

JG

jgerard at fluid-studios.ca (JM) wrote in message news:<fcd83a69.0308261502.56809870 at posting.google.com>...
> Hello everyone,
> 
> Does anybody know about, have documentation on, or have any code
> samples on how to access class members from a python class in C++.
> 
> Say I have a simple python script:
> 
> ---------------------------
> class Animal:
>     NumLegs = 5
>     Size = 4.5
> ---------------------------
> 
> How exactly do you access these members from C++.  I know how to get
> the member if I know its name, ie.
> 
> pkObject = PyDict_GetItemString(pkDict, "Animal");
> int uiNumLegs  = PyInt_AsLong(PyObject_GetAttrString(pkObject,"NumLegs"));
> 
> But how do you iterate through all the class members and print their
> names and values? This is easy to do for basic variables, but I can't
> seem to do it for class variables.
> 
> Any help would be much apprecited. This is driving me nuts.
> 
> JG




More information about the Python-list mailing list