# [Python-ideas] Pass a function as the argument "step" of range()

Nathaniel Smith njs at pobox.com
Thu Jul 2 09:12:46 CEST 2015

```On Jul 1, 2015 23:56, "Ben Finney" <ben+python at benfinney.id.au> wrote:
>
> Pierre Quentel <pierre.quentel at gmail.com>
> writes:
>
> > To achieve the same thing in Python we currently can't use range()
> > because it increments by an integer (the argument "step"). An option
> > is to build a generator like :
> >
> > def gen(N):
> >     i = 1
> >     while i<=N:
> >         yield i
> >         i *= 2
> >
> > then we can iterate on gen(N).
>
> Generators can be defined in expressions, of course::
>
>     ((x * 2) for x in range(n))
>
> So the full function definition above is misleading for this example.
>
> Your single example defines the ‘step’ function in-line as a lambda
> expression::
>
> > Iterating on the powers of 2 below N would be done by :
> >
> > for i in Range(1, N, lambda x:x*2)
>
> So why not define the generator as an expression::
>
>     for i in ((x * 2) for x in range(n)):
>
> That seems quite clear given existing syntax.

I believe the original example was actually

for i in ((2 ** (x + 1) for x in range(int(log2(n)))):

or similar... which is clearly making some sort of argument about clarity
but I'm not sure what.

This isn't going to work for range() anyway though AFAICT because range
isn't an iterator, it's an iterable that offers O(1) membership tests.

I could see an argument for putting something along these lines in
itertools. itertools.orbit, maybe. But I've never felt an urgent need for
such a thing myself.

-n
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150702/b9e10de6/attachment-0001.html>
```