Two way communication between a python class and a C++ class
Dave Kuhlman
dkuhlman at rexx.com
Fri Nov 9 12:49:40 EST 2001
What you are looking for (if I understand you) is a way to pass an
instance of a C++ class to Python so that the Python code can call
a method in that class.
Stop me if I'm wrong about that ...
But, if I'm right, then consider doing the following:
Implement a new Python data type. Implement it in C/C++. An
instance of this data type will hold (internally) a pointer to the
instance of your C++ class, and it will "expose" to Python one or
more methods, which will call the methods in (the instance of) your
C++ class.
Think about the list built-in class in Python. It wraps a C list
object, and when you do:
mylist = []
mylist.append('hello')
It calls an append function (implemented in C) for that object.
How to implement it -- Get the Python source distribution. Look in
the Objects subdirectory for a file Objects/xxobject.c. Copy and
rename that file. Then follow the suggestions in the comments at
the top of that file.
Also look for help in:
http://www.python.org/doc/current/ext/defining-new-types.html
I've also written a tool to help generate the source code for
simple cases where little more than a wrapper for a C struct is
needed. It's called dtGenerator.py and you can find out about it
at:
http://www.rexx.com/~dkuhlman/
Hope this helps. And, let us know if/when you have more questions.
- Dave
Firestar <dwjoker at thenorth.org> wrote:
>
> Hi,
>
> I am having a problem obtaining two way communication between a python
> class and a c++ class. I can get a python class to communcate with a
> c++ class and a c++ class to communicate with a python class, but i
> cannot seem to get the communication to go both ways.
>
> i.e I have a class CTest in c++ and a class PTest in python and when i
> create an instance of CTest in c++ it creates an instance of PTest in
> python, i then want PTest to be able to call methods in the instance
> of CTest that created the instance of PTest. How can i do this? there
> seems to be no way to pass the c++ pointer into the python class in
> the constructor...
>
> There must be a way... does anyone know how to do this??? Help much
> appreciated!!
>
> Firestar
--
Dave Kuhlman
dkuhlman at rexx.com
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 80,000 Newsgroups - 16 Different Servers! =-----
More information about the Python-list
mailing list