Re: [ROOT] PyROOT and STL containers.

From: Brett Viren (bv@bnl.gov)
Date: Sun Oct 31 2004 - 21:02:46 MET


WLavrijsen@lbl.gov writes:

>> I have a class that reutrns an "std::vector<std::string>".  Some of
>> the std::vector methods work but some don't, particularly operator[].
>
> Do you have at() available? 

Nope.

AttributeError: 'vector<string>' object has no attribute 'at'

Is this some extension to the C++ stdlib?  I don't find it in the STL
docs (the ones from SGI).

> The problem here is that there's no Python
> equivalent of "T& operator(int)[]". There is for the const version (where
> a T* rather than T& will do the trick), but it is hard to disentangle the
> two if they are both available.

Yes, I guessed this might be the source of the problem.

What would be totally great is if the wrapped STL containers (and for
that matter, ROOT ones as well) acted like native Python containers.
Ie, to be able to do:

for n in return_stl_list():
    use_name(n)

It seems that this should certainly be possible but I don't know with
what difficulty.  For STL vector and list, mapping __getitem__ to
operator[]() and __len__ to size() would go a long ways to making
these classes useable.  A little more tricky would be to have
__iter__() return another wrapper that maps next() to
vector::iterator::operator++, etc.  STL map would be similar, although
with more custom coding needed.

SWIG (www.swig.org) wraps STL containers like this.  If you are
familiar with this program maybe the interface files python/std_*.i in
will give some useful guidance on this.


Thanks and keep up the good work,
-Brett.



This archive was generated by hypermail 2b29 : Sun Jan 02 2005 - 05:50:10 MET