[Python-ideas] Simpler syntax for basic iterations

Luciano Ramalho luciano at ramalho.org
Sun Oct 11 23:47:10 CEST 2015


I've felt the same need Andre described, and so far I have not seen a
convincing argument against his proposal to accept this syntax:

for n:  # repeat block n times
     fd(100)
     lt(360/n)

Where n is any expression that evaluates to an integer.

The goal is to repeat some operation N times without creating a
spurious variable. I prefer this simpler alternative than Ian's later
proposal where n must be an iterable, involving a lot of machinery
just to produce a value to be discarded.

While we are at it, we could borrow Go's "forever" loop:

for:  # repeat forever
    spam()

Although I am quite happy with `while True:` for that purpose.

Cheers,

Luciano




On Sun, Oct 11, 2015 at 4:48 AM, Stephen J. Turnbull <stephen at xemacs.org> wrote:
> Andre Roberge writes:
>
>  > >> While Python is an ideal language for beginners, the Python
>  > >> construct for iterations, namely
>  > >>
>  > >> for var in range(n):
>  > >>      # suite
>  > >>
>  > >> does require a variable and is needlessly complicated for
>  > >> beginners.  A simpler construct like:
>  > >>
>  > >> repeat n:    # n is a specific integer
>  > >>      # suite
>  > >>
>  > >> would be useful to have in such contexts.  Other keywords could
>  > >> be chosen instead of "repeat" used above.
>
> I have no objection to such a language, but it ain't Python.  It's an
> important principle of Python that "There's one- and preferably only
> one -obvious way to do it."  While the parenthetical "only one" is
> violated on occasion, it's still considered important.
>
>  > >> for var in range(n):
>  > >>      # suite
>
> I just don't have a problem with this.
>
> It seems to me that on the occasions when I've taught programming[2],
> I've always had the most success when the student has a concrete
> problem in mind (sometimes they need to write programs to complete
> another project, but also when they take programming as a required
> course).  Take the traditional make a square in turtle graphics:
>
>     right = -90
>     length = 10
>     def make_square (turtle):
>         for side in ('top', 'right', 'bottom', 'left'):
>             turtle.forward(length)
>             turtle.turn(right)
>
> Do you really have students writing code like
>
>     for _ in range(20):
>         print('yes')
>
> where the repetition is exact?
>
>  > It is unfortunate, given the original CP4E goal, but not altogether
>  > unexpected -- although, I do find the word "mutilate" to be a bit
>  > strong.
>
> The goal called "CP4E" as I understood it was to give the *full* power
> of "computer programming" to "everyone", not to just provide a taste.
> The *power* of computer programming comes from small but expressive
> vocabularies, it seems to me.
>
>  > The reason is that I don't believe in students having to unlearn
>  > something (like a special repeat syntax available only in a
>  > specific environment) when they might have been under the
>  > impression that it was standard Python syntax.
>
> So call the new language "Childrens-Python"[1] if you like.<wink/>  Or
> "Woma" (which is a bit bigger than Children's Python) if that's too
> close to "Python".
>
>  > Furthermore, it might instill doubt in their mind about other
>  > construct they have learn (are they standard? ... or were they too
>  > something only available in the specific environment?...)
>
> So what?  If they're going to do computer programming after they leave
> your class, they're surely going to encounter "specific environments".
> So many times I've typed an example into the interpreter only to
> discover that the author didn't explicitly import a function or
> identifier from a module, but it looks like it could be a builtin I
> hadn't used before.
>
>  > I'll have a look.  However, as I mentioned above, I'm not keen on
>  > introducing what amounts to a potentially completely different
>  > language.
>
> It doesn't look to me like that's what Terry is talking about, though.
> I suppose that as usual there will be a "consenting adults" convention
> that "you could do that with this hook, but please, *not* in front of
> the children!"  Rather, for cases where you don't need the full power
> of Python's expression syntax, you could eliminate parentheses from
> function calls and that kind of thing.
>
>  > Python is a fantastic language for beginners (and advanced)
>  > programmers ...  I was just hoping to make it a tiny bit easier to
>  > learn for complete beginners who are introduced to Python in visual
>  > environments (like the turtle module or a Karel the Robot clone).
>
> At the cost of requiring every Python programmer to beware of a new
> and infrequently syntax.
>
>
> Footnotes:
> [1]  http://www.snakeranch.com.au/snake-profiles/childrens-python/
>
> [2]  Always at the college level or above, though.
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/



-- 
Luciano Ramalho
|  Author of Fluent Python (O'Reilly, 2015)
|     http://shop.oreilly.com/product/0636920032519.do
|  Professor em: http://python.pro.br
|  Twitter: @ramalhoorg


More information about the Python-ideas mailing list