[Python-ideas] Way to repeat other than "for _ in range(x)"

Markus Meskanen markusmeskanen at gmail.com
Thu Mar 30 13:38:57 EDT 2017


On Thu, Mar 30, 2017 at 8:23 PM, Brice PARENT <contact at brice.xyz> wrote:

>
> Le 30/03/17 à 19:06, Markus Meskanen a écrit :
>
>> And like I said before, for loop is just another way of doing while loop,
>> yet nobody's complaining. There's nothing wrong with having two different
>> ways of doing the same thing, as long as one of them is never the better
>> way. If we add `repeat`, there's never a reason to use `for _ in range`
>> anymore.
>>
> It doesn't always creates something easier to use, like for example :
> `for _ in range(x, y, z)` (fixed or variable parameters)
> `for _ in one_list` (saves a call to len() with your solution)
> `for _ in any_other_kind_of_iterable` (we don't need to know the length
> here, we may even use a generator)


Your first example:

> `for _ in range(x, y, z)`

Makes little sense, since there's still a fixed amount of steps and normal
range(x) could just be used instead. As a matter of fact, it can be
replaced with either of these, arguably `repeat_for` version being cleaner:

  for _ in range((y-x+1)//z)
  repeat_for (y - x + 1) // z

And in that one *extremely* unlikely and rare scenario where someone really
does need range() with variable start, stop, and step, and doesn't need the
returned variable, he can freely still use `for _ in range`. This won't
remove it.

Your other two examples:

> `for _ in one_list`
> `for _ in any_other_kind_of_iterable`

Aren't related to the syntax I'm proposing, you even quoted this part
yourself:

> there's never a reason to use `for _ in range` anymore.

But your examples don't use range() to begin with.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170330/1f3b0b33/attachment.html>


More information about the Python-ideas mailing list