[C++-sig] Re: Re: Re: custom iterator object
Mike Rovner
mike at bindkey.com
Mon Nov 11 23:44:39 CET 2002
> 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?
PyIter make_iter(const Scheme* s, GetFunc f) { return s->(*f)(); }
...
class_<Scheme>("Scheme")
.def("__iter__", make_iter(this,&Scheme::A))
.add_property("b", make_iter(this,&Scheme::B))
.add_property("c", make_iter(this,&Scheme::C))
;
Mike
More information about the Cplusplus-sig
mailing list