[C++-sig] Boost.python generators

John Reid j.reid at mail.cryst.bbk.ac.uk
Mon Jan 8 19:08:54 CET 2007


Thinking about it I guess the best thing to do is just to implement
__iter__ and next and throw a StopIteration when done.

So if I'm right I could rewrite the code below in C++. This would be 
equivalent to a generator.

Cheers,
John.


import random
class randomwalk_iter:
     def __init__(self):
         self.last = 1               # init the prior value
         self.rand = random.random() # init a candidate value
     def __iter__(self):
         return self                 # simplest iterator creation
     def next(self):
         if self.rand < 0.1:         # threshhold terminator
             raise StopIteration     # end of iteration
         else:                       # look for usable candidate
             while abs(self.last-self.rand) < 0.4:
                 print '*',          # display the rejection
                 self.rand = random.random() # new candidate
             self.last = self.rand   # update prior value
             return self.rand



John Reid wrote:
> Hi,
> 
> I've had a quick look but found nothing that lets me implement a 
> generator in C++ and export it with boost.python. Is this possible?
> 
> Thx,
> John.




More information about the Cplusplus-sig mailing list