# Write a range replacement (adjrange, short for "adjustable range"), # which can be prodded with .send() to change the step. # # This could be useful e.g. in a numerical integration routine, where # we want to increase the step size in boring areas, and decrease in # areas of high variability. def adjrange(start, stop, step): """A range()/xrange() replacement with adjustable step. >>> x = adjrange(0, 7, 1) >>> next(g) 0 >>> next(g) 1 >>> g.send(2) >>> next(g) 3 >>> next(g) 5 >>> next(g) Traceback: ... StopIteration: ... """ while start < stop: val = yield start if val is None: start += step else: step = val # Bonus: fix args so that adjrange(stop) works