title: For loop with step
body: <p>I have created a python script that can loop over a range of values based on a specified step function.</p>
  
  <pre><code>#!/usr/bin/python
  
  def mul(value, step): return value * step
  def inc(value, step): return value + step
  def step_range(start, end, step, func):
      while start &lt;= end:
          yield start
          start = func(start, step)
  
  def main():
      for x in step_range(1, 64, 2, mul): 
          print '%d, '%(x),
      print
      for x in step_range(0, 64, 8, inc): 
          print '%d, '%(x),
  
  if __name__ == '__main__':
      main()
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>$ python test.py
  1,  2,  4,  8,  16,  32,  64,
  0,  8,  16,  24,  32,  40,  48,  56,  64,
  </code></pre>
  
  <p>Is there anyway I can get rid of the helper functions so that the user can do something like this?</p>
  
  <pre><code>for x in step_range(1, 64, *2): 
      ...
  
  def step_range(start, end, step):
      while start &lt;= end:
          yield start
          start = start ?step?
  </code></pre>
  
  <p>The <code>?</code> marks where I am stumped... I looked into the <a href="http://docs.python.org/2/library/operator.html" rel="nofollow"><code>operator</code></a> module but I would have to know both parameters for the <code>mul(a, b)</code> and <code>add(a, b)</code> functions.</p>