Python wrapper for C++ STL?

Alex Martelli aleaxit at yahoo.com
Sat Dec 9 13:57:54 CET 2000


"Edward C. Jones" <edcjones at erols.com> wrote in message
news:3A31A05C.6A853969 at erols.com...
> shindich at my-deja.com wrote:
>
> > With all due respect, what would be wrong with a definition like
> > std::map<PyObject*, PyObject*>? Obviously there is no type safety here.
> > But Python is not type safe.
>
> This seems correct. It would give me a "sorted associative container", a
> dictionary I could loop through.

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'...:-).


Alex






More information about the Python-list mailing list