Python wrapper for C++ STL?

David Abrahams abrahams at mediaone.net
Sat Dec 9 12:04:39 EST 2000


"Alex Martelli" <aleaxit at yahoo.com> wrote in message
news:90taaj02qjj at news2.newsguy.com...

> Hmmm, yes, but, take care: the 'loop-through' feature had better be
> exposed through a specially-named method or attribute -- I think it
> would be better if .keys() returned a normal Python list, as a caller
> would expect from normal polymorphic behavior, and while it's nice
> that this list is sorted rather than randomish'ly unordered, it can be
> a substantial overhead to build it.
>
> And you can't just use:
>     for key in myFuncyObject:
>         whatever(key)
> as that would require the object to be indexable by 0, 1, ..., n with
> the meaning of 'loop through it', interfering with the normal meaning
> (as these integers can be perfectly normal dictionary-keys!-).
>
> The design pattern to implement the indexability thus requires an
> auxiliary object (and a little bit of care in it, since Python for-loop
> protocol is based on a growing int index, and you have to mutate
> that, internally, into a sequential 'current iterator'/'increment it'
> pattern).  No rocket science, mind you, but it's not quite as obvious
> as _just_ 'wrapping a std::map'...:-).

The Boost Python Library to be released this weekend under that moniker
(formerly py_cpp) has a feature in the pipeline allows easy wrapping of STL
containers. Even non-random-access containers can be used efficiently with

    for element in container.cursor():
        whatever(element)

This ingenious feature was designed by Ullrich Koethe. It won't be in the
first release, but probably will appear within the next few weeks
thereafter.

Regards,
Dave





More information about the Python-list mailing list