<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">2015-07-02 9:32 GMT+02:00 Andrew Barnert via Python-ideas <span dir="ltr"><<a href="mailto:python-ideas@python.org" target="_blank">python-ideas@python.org</a>></span>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="auto"><div><div class="h5"><div>LOn Jul 2, 2015, at 00:12, Nathaniel Smith <<a href="mailto:njs@pobox.com" target="_blank">njs@pobox.com</a>> wrote:</div><div><br></div><blockquote type="cite"><div><p dir="ltr">On Jul 1, 2015 23:56, "Ben Finney" <<a href="mailto:ben%2Bpython@benfinney.id.au" target="_blank">ben+python@benfinney.id.au</a>> wrote:<br>
><br>
> Pierre Quentel <<a href="mailto:pierre.quentel@gmail.com" target="_blank">pierre.quentel@gmail.com</a>><br>
> writes:<br>
><br>
> > To achieve the same thing in Python we currently can't use range()<br>
> > because it increments by an integer (the argument "step"). An option<br>
> > is to build a generator like :<br>
> ><br>
> > def gen(N):<br>
> >     i = 1<br>
> >     while i<=N:<br>
> >         yield i<br>
> >         i *= 2<br>
> ><br>
> > then we can iterate on gen(N).<br>
><br>
> Generators can be defined in expressions, of course::<br>
><br>
>     ((x * 2) for x in range(n))<br>
><br>
> So the full function definition above is misleading for this example.<br>
><br>
> Your single example defines the ‘step’ function in-line as a lambda<br>
> expression::<br>
><br>
> > Iterating on the powers of 2 below N would be done by :<br>
> ><br>
> > for i in Range(1, N, lambda x:x*2)<br>
><br>
> So why not define the generator as an expression::<br>
><br>
>     for i in ((x * 2) for x in range(n)):<br>
><br>
> That seems quite clear given existing syntax.</p>
<p dir="ltr">I believe the original example was actually</p>
<p dir="ltr">for i in ((2 ** (x + 1) for x in range(int(log2(n)))):</p>
<p dir="ltr">or similar... which is clearly making some sort of argument about clarity but I'm not sure what.</p>
<p dir="ltr">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.</p>
<p dir="ltr">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.</p></div></blockquote></div></div><div>You can already do this with accumulate; you just have to write lambda x, _: x*2.</div><div><br></div><div>Of course it doesn't include the built-in bounds, but I don't think you'd want that anyway. With accumulate, you can bound on the domain by passing range instead of count for the input, bound on the range with takewhile, or generate an infinite iterator, or anything else you think might be useful.</div><br><div>Or one more of the various combinations of things you can trivially build out of these pieces might be useful as a recipe ("irange"?) and/or in the third-party more-iterools.</div></div><br>_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br></blockquote><div><br>I am not saying that you can't find other ways to get the same result, just that using a function (usually a lambda) is easier to code and to understand.<br><br>The proposal would bring to Python one of the few options where iteration is more simple in Java or Javascript than with Python - I had the idea from this discussion on reddit : <a href="https://www.reddit.com/r/Python/comments/3bj5dh/for_loop_with_iteratively_doubling_range/">https://www.reddit.com/r/Python/comments/3bj5dh/for_loop_with_iteratively_doubling_range/</a><br><br> </div></div><br></div></div>