generator object or 'send' method?

greg greg at cosc.canterbury.ac.nz
Wed Feb 11 11:41:22 CET 2009


Aaron Brady wrote:

> It would receive the 'send' from a different point in control
> flow than its usual 'next'.  Should it repeat a value if it receives a
> 'send'?

No. This requirement clearly indicates that send() is
the wrong tool for the job.

I would use a class with a generator as a method, e.g.

   class Multiples:

     m = 1

     def set_multiplier(self, m):
       self.m = m

     def generate(self, limit):
       for i in xrange(limit):
         yield i * self.m

   g = Multiples()
   for x in g.generate(10):
     print x
     if x == 3:
       g.set_multiplier(42)

produces

0
1
2
3
168
210
252
294
336
378

-- 
Greg



More information about the Python-list mailing list