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

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

JM
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
Jul 18 '05 #1
2 4186
JM
If anyone is interested in the solution:

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

JG

jg*****@fluid-studios.ca (JM) wrote in message news:<fc**************************@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,"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

Jul 18 '05 #2
On 27 Aug 2003 10:14:02 -0700, jg*****@fluid-studios.ca (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

Jul 18 '05 #3

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

Similar topics

2
by: disgracelands | last post by:
I'm starting to develop an application in Python running the interpreter from C, i've got callbacks and stuff setup (was pretty pleased with that) and i'm starting to get my head around the...
0
by: Carlos Ribeiro | last post by:
I thought about this problem over the weekend, after long hours of hacking some metaclasses to allow me to express some real case data structures as Python classes. I think that this is something...
20
by: Ilias Lazaridis | last post by:
" A cooperation between Sun Microsystems and IBM&Co. in conjunction with liberal & high evolutive communities would result in an nearly unbeatable programming platform. My evaluation has shown:...
5
by: Suzanne Vogel | last post by:
Hi, Given: I have a class with protected or private data members, some of them without accessor methods. It's someone else's class, so I can't change it. (eg, I can't add accessor methods to the...
5
by: Sandeep | last post by:
Hi, In the following code, I wonder how a private member of the class is being accessed. The code compiles well in Visual Studio 6.0. class Sample { private: int x; public:
2
by: Steven T. Hatton | last post by:
I find the surprising. If I derive Rectangle from Point, I can access the members of Point inherited by Rectangle _IF_ they are actually members of a Rectangle. If I have a member of type Point...
2
by: Brett Styles | last post by:
Hi Guys, I am trying to access a class in an unmanaged dll. I have created a wrapper managed class to access the functions I need but no matter what I try from the MSDN samples I can not get it to...
6
by: earthwormgaz | last post by:
Is the following legal? class Outer { class Inner { private: Inner() { } };
3
by: dmoore | last post by:
Hi Folks: I have a question about the use of static members in Python/C extensions. Take the simple example from the "Extending and Embedding the Python Interpreter" docs: A simple module...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.