Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Accessing members of Python class from C++ (Embedded Python)????

Reply
Thread Tools

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

 
 
JM
Guest
Posts: n/a
 
      08-26-2003
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,"NumL egs"));

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
 
Reply With Quote
 
 
 
 
JM
Guest
Posts: n/a
 
      08-27-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

http://www.velocityreviews.com/forums/(E-Mail Removed) (JM) wrote in message news:<(E-Mail Removed). 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,"NumL egs"));
>
> 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

 
Reply With Quote
 
 
 
 
Greg Chapman
Guest
Posts: n/a
 
      08-28-2003
On 27 Aug 2003 10:14:02 -0700, (E-Mail Removed) (JM) wrote:

>If anyone is interested in the solution:
>
>pDict=PyObject_GetAttrString(PyObject,"__dict__ ") will get the local
>symbol table (a dictionary) for the class.


This may not be relevant to your needs, but a class's __dict__ only has
references to the attributes actually declared in the class; it doesn't have
inherited attributes. If you need access to all the attributes of a class, use
PyObject_Dir(aClass), which returns a list of all attribute names declared in
the class and it's superclasses (it's the C equivalent of the dir builtin). You
can then use PyObject_GetAttr(aClass, attrname) to fetch the attributes
themselves.

---
Greg Chapman

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessing derived class members from base class Bhawna C++ 7 08-26-2008 11:03 AM
class members vs instance members hdixon Python 3 07-09-2006 06:56 PM
Accessing shadowed members of an enclosing class Bernd Fuhrmann Java 3 10-15-2005 09:36 PM
Can nested class members access private members of nesting class? CoolPint C++ 8 12-14-2003 02:30 PM
accessing members of a templated base class John Harrison C++ 18 08-10-2003 11:41 PM



Advertisments