Re: how to make a generator use the last yielded value when it regains control



John Salerno wrote:
Actually I was just thinking about this and it seems like, at least for
my purpose (to simply return a list of numbers), I don't need a
generator.

Yes, if it's just a list of numbers you need, a generator is more
flexibility than you need. A generator would only come in handy if,
say, you wanted to give your users the option of getting the next N
items in the sequence, *without* having to recompute everything from
scratch.


My understanding of a generator is that you do something to
each yielded value before returning to the generator (so that you might
not return at all),

A generator is just an object that spits out values upon request; it
doesn't care what the caller does with those values.

There's many different ways to use generators; a few examples:

# Get a list of the first 10
from itertools import islice
m = [n for n in islice(morris(1), 10)]

# Prompt user between each iteration
for n in morris(1):
if raw_input('keep going? ') != 'y':
break
print n

# Alternate way of writing the above
g = morris(1)
while raw_input('keep going? ') == 'y':
print g.next()

--Ben

.



Relevant Pages

  • Re: Good enough for crypto?
    ... > swilber@comscire.com (Scott Wilber) writes: ... >> purpose of attacking. ... Resources) claims that it 'seems to be the only generator specifically ... First, 30,000 random bytes were generated using a ComScire QNG ...
    (sci.crypt)
  • Re: More Generator stuff
    ... much as listening to a generator. ... What I require, above all, is that it be QUIET. ... I'm not sure I understand what the purpose of that post was, ... that an inverter generator is a panacea for all generator-related issues. ...
    (rec.outdoors.rv-travel)
  • Re: Automatic VHDL Generating
    ... Another purpose might be the generation of state machines. ... If you have a very special problem where no tool exists, that maps this problem to VHDL you can "easily" write your own generator. ... This "generator" is nothing more than an ASCII text parser and ASCII text generator. ...
    (comp.lang.vhdl)
  • Re: Testing a Pseudo-Random Generator
    ... Bob wrote: ... are there a set of tests that could assure us that the Generator is fair, ... The Generators are “specialised” i.e. good for one purpose bad for the others? ... what you think about the randoms independence? ...
    (sci.stat.math)
  • Re: how to make a generator use the last yielded value when it regains control
    ... John Salerno wrote: ... implement as a generator. ... >>> from itertools import groupby ... If you want to get just part of the infinite series, ...
    (comp.lang.python)