[Python-ideas] Simpler syntax for basic iterations

Chris Barker - NOAA Federal chris.barker at noaa.gov
Sat Oct 10 01:33:54 CEST 2015


> Or, to make it look that little bit more "natural", spell it:
>
> for each in range(n):
>  # magic

Indeed -- while python's for loop can be (and often is) used to repeat
something a certain number of times, what it really is is a way to
apply a block of code to each item in an iterable.

Making a slightly shorter/easier way to do something n times will just
make it harder to "get" what for loops really are later on.

And aside from using it as a teaching tool, does one want to loop
without using the index all that often to get special syntax?

The Karel-style simplified languages may be a great teaching tool --
but that's not what Python is.

-CHB



> ... and then one day, move on to explain that "each" is actually something you can USE rather than just being a fixed part of the syntax - at which point it can be spelled better according to what it actually is.
>
> An alternative suggestion to remove the need for the variable (rather than a new keyword) would be something like allowing the "var in" part to be optioinal:
>
> for <iterator>:
>  # magic
>
> ... but the problem with that is that pretty much the ONLY use would be for this sort of "iterate n times" construct or something where you're relying on the iterator to have side-effects (which is pretty nasty):
>
> foo = myiter()
> for foo:
>  bar(foo.getcurrentvalue())
>
> It seems like a lot of effort just to hide a bit of syntax for a very specific use-case when you can just temporarily make that syntax look a bit nicer for your audience.
>
> E.
> _______________________________________________
> 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/


More information about the Python-ideas mailing list