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

Rhodri James rhodri at kynesim.co.uk
Thu Mar 30 13:49:12 EDT 2017


On 30/03/17 18:06, Markus Meskanen wrote:
> 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.

That's a C-programmer point of view.  In C, it's true;

   for (init(); cond(); inc()) { ... }

is just a convenient form of

   init();
   while (cond()) { ...; inc(); }

(ignoring breaks and continues)

In Python, the two types of loop are conceptually rather more different. 
  A while loop loops based on a condition; a for loop iterates through 
an iterable.  Doing simple repetition as "for _ in range(x)" is a bit 
artificial really, but less ugly than doing it with a while loop.

Your proposed "repeat" (however it is spelt) is a special case, and a 
pretty limited one at that.  I'm not sure I've needed it, certainly not 
for a while, and I have to say I don't find array initialisation a 
compelling use-case.  I really don't like the idea of finding it in 
comprehensions.

-- 
Rhodri James *-* Kynesim Ltd


More information about the Python-ideas mailing list