[C++-sig] Re: C arrays mk 2

Raoul Gough RaoulGough at yahoo.co.uk
Wed Oct 15 23:24:30 CEST 2003


David Abrahams <dave at boost-consulting.com> writes:

> Raoul Gough wrote:
[snip]
>> I suppose it depends how rich an interface he wants on the Python
>> side. The two methods you named are sufficient for a lot of cases
>> (almost everything, in fact), but if he wants slice support or
>> __setitem__, it might still pay off to use the container suite.
>
> Sure.  But his example just creates an iterator (the extremely hard
> way), and __len__ + __getitem__ already gets you an iterator and more.

That's right - it's neat how many Python expressions can work with
just those two methods (in fact, I think __len__ is not even necessary
for much of it, since IndexError works as well as StopIteration).
However, I think his code actually does enlist some extra
container_suite support via the iterator_pair container emulation,
which goes one or two steps further. The macros obscure it somewhat,
but I think it basically does this:

typedef indexing::iterator_pair<memberType *> IterPair;

class_<IterPair>("Name", init<...>)
  .def (indexing::container_suite<IterPair>());

Which usually gives you __getitem__ and __setitem__ with slice
support, as well as __len__, index, count, sort and reverse. Now
whether you actually need all that stuff is another question!

-- 
Raoul Gough.
(setq dabbrev-case-fold-search nil)





More information about the Cplusplus-sig mailing list