[PYTHON C++-SIG] Python/STL thoughts

Johann Hibschman johann at physics.berkeley.edu
Wed Feb 19 08:16:07 CET 1997


On Tue, 18 Feb 1997, Geoffrey Furnish wrote:

> Johann Hibschman writes:
>  > Personally, I was thinking about gradually implementing a STL-to-Python 
>  > conversion, as I needed it in the code I'm writing for my own use.  Does 
>  > anyone think this is a good idea?  If not, why not?
> 
> Can you define this more precisely?  I can't quite tell what you mean.

Well, as I see it, there are three ways to think of integrating Python and 
C++ data structures:

   1. For primarily C++ programs, a program should be able to take STL 
container objects and copy them into Python lists or dictionaries, allow 
the user to manipulate these objects, and then copy the Python 
representation into a native C++ representation.

   2. For Python extensions that happen to be written in C++, the program 
should be able to create an STL-like object which manipulates the actual 
Python list/dictionary through calls to PyList_Insert, PyList_GetItem, 
etc., without actually copying the container over into a C++ native form.
i.e. the STL list contains type PyObject *, which the program must then 
explicitly handle.

   3. Create a C++ extension which properly defines the Python sequence 
or dictionary methods, so the native C++ representation internal to the 
code is manipulated by Python-level commands.

I was thinking of (1), namely a way to easily make data structures in a 
normal C++ program visible to Python without requiring major changes to 
the original C++ program.  This would be for interactive debugging and 
data monitoring use.  (Eventually I'd want to do this to NumPy arrays, 
but if I can get it into a list, I can get it into an array.)

Category (2) is the foundation for most of the work in (1).  I envision 
being able to do something like:

pythonlist = <some way to get a reference to a python list>;
copy( pythonlist.begin(), pythonlist.end(), cpplist.begin() );

to copy over data items from a python object to a c++ object.  Of course, 
the problem is now how to tell the python list what kind of object it 
supposed to contain, since the c++ lists want homogeneous objects, while 
a python list could contain just about anything.

Perhaps an STL-style iterator adaptor would be appropriate, as in

copy( pythonlist.begin(), pythonlist.end(), 
      PyAdaptor<double>(cpplist.begin()) );

where the "PyAdaptor" would contain the logic to cast the PyObject 
pointers in the Python list to doubles, and raise an exception if any of 
the objects in the list was not a double. 

Category (3) is mostly for full-featured language extensions, although a
Python wrapper for the STL container classes might be useful.  For
example, maybe a linked-list representation of a list might be more
efficient for some task than the standard Python array implementation,
since it would allow efficient insertions and deletions.  A Python wrapper
which uses STL for its functionality could do that fairly easily. 

As you can see, I have a number of ideas about how this could work, but
few specifics.  I'd appreciate anyone's comments about what they think is
the most common role of transferring data in Python extensions, about the
feasibility of using STL-based containers, and about any relevant ideas 
whatsoever.

Cheers,

- Johann

---
Johann Hibschman
johann at physics.berkeley.edu   


_______________
C++-SIG - SIG for Development of a C++ Binding to Python

send messages to: c++-sig at python.org
administrivia to: c++-sig-request at python.org
_______________



More information about the Cplusplus-sig mailing list