473,656 Members | 2,921 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_GetItemS tring(pkDict, "Animal");
int uiNumLegs = PyInt_AsLong(Py Object_GetAttrS tring(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
Jul 18 '05 #1
2 4196
JM
If anyone is interested in the solution:

pDict=PyObject_ GetAttrString(P yObject,"__dict __") will get the local
symbol table (a dictionary) for the class.

JG

jg*****@fluid-studios.ca (JM) wrote in message news:<fc******* *************** ****@posting.go ogle.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_GetItemS tring(pkDict, "Animal");
int uiNumLegs = PyInt_AsLong(Py Object_GetAttrS tring(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

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,"__dic t__") 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(aC lass), 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_GetAtt r(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
1966
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 reference counting but i've hit a snag. I'm creating objects within python then linking them to their C counterparts, what i want to do now is directly access the python class variable objects from C. Basically i have the python class object as a C...
0
2060
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 with potential to be useful, but I would like to hear more opinions first. If this is deemed to be useful, I *may* try to write a PEP for it. This is not a promise or even a proposal, at this point. Broadly generalizing, classes in Python have...
20
2330
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: this is a non achievable goal, as stubborness and egoism rules - instead of reason and cooperation. Thus I leave all those ridiculous folks behind, which will continue to do an excellent job in keeping the very promising JAVA platform far
5
3645
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 parent class, and I can't make some "helper" class a friend of the parent class to help in accessing the data.) Problem: I want to derive a class that has a copy constructor that properly copies those data members.
5
2408
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
2297
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 in Rectangle, the compiler tells me Point::x is protected. I would have expected Rectangle to see the protected members of any Point. Compiling the following code give me this error: g++ -o rectangle main.cc main.cc: In member function `size_t...
2
2408
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 work with my code. I have a VB Net front end which need the access the unmanaged functions. The wrapper I wrote can be accessed but the wrapper will not compile when I refer to the unmanaged class. I have tried using header file for definitions...
6
5028
by: earthwormgaz | last post by:
Is the following legal? class Outer { class Inner { private: Inner() { } };
3
2680
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 method: static PyObject * spam_system(PyObject *self, PyObject *args)
0
8816
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8498
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8600
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1930
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1600
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.