[Python-ideas] Simpler syntax for basic iterations

Erik python at lucidity.plus.com
Fri Oct 9 23:27:11 CEST 2015


On 09/10/15 21:27, Ryan Gonzalez wrote:
> Just use:
>
> for _ in range(n):
>      # magic here
>
> and tell them they'll figure out what the _ is later on. That's how I learned it...

Or, to make it look that little bit more "natural", spell it:

for each in range(n):
   # magic

... 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.


More information about the Python-ideas mailing list