[C++-sig] Re: Re: Re: custom iterator object

David Abrahams dave at boost-consulting.com
Tue Nov 12 02:34:55 CET 2002


"Mike Rovner" <mike at bindkey.com> writes:

>> Well, you can't use range(...) here; that's intended for real C++
>> iterators, and you don't have any (PyIter doesn't conform).
>
> (Aside question) Why?
> To put it another way, how to write a conforming or use an iterator_adaptor?
> I was convinced that that way will be more clear and maintainable.
>
>> Here's my suggestion:
>>
>>   Try doing what you need to do in Pure Python. Build a little
>>   new-style class (derived from object) called Scheme, and add the
>>   iterator interface you want to see. To do that, you may find
>>   yourself building some little Python iterator classes.
>
> Like that:
>
> class Iter:
>   def __init__(self,a): self.a=a()
>   def __iter__(self): return self
>   def next(self): return self.a
>
> class Scheme:
>   def __iter__(self): return Iter(self.A)
>   def i2(self): return Iter(self.B)
>   def i3(self): return Iter(self.C)
>   def A(self): return 1
>   def B(self): return 2
>   def C(self): return 3
>
>>   Now, when you have all that working, translate your Python iterator
>>   classes into C++, and wrap them with Boost.Python. Remember that the
>
> Python code above working but I still have problems with C++:
> How can I pass 'this' from wrapping class?


Didn't I write this?

> >   Remember that the iterator's own __iter__ method can be
> >   implemented by wrapping this C++ function:
> >
> >     object identity(object x)  { return x; }

Did I leave something out? You can use:

    .def("__iter__", identity)

inside your iterator wrapper class. Does that make it clear? Or are
you really asking something else?

-- 
                       David Abrahams
   dave at boost-consulting.com * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution





More information about the Cplusplus-sig mailing list