[Python-ideas] Simpler syntax for basic iterations

Ian ian.team.python at gmail.com
Sun Oct 11 04:55:58 CEST 2015


if anything is changed at all, then the

for <iterator>:
  # magic


seems to have the least impact and best addresses the original goal.


repeat n:  # saves 4 characters
     vs
for range(n):  # keeps syntax consistent

Which (consistency vs save 4 characters) would you favour for a beginner?
Not to mention that introducing a new keyword introduces many challenges.

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



More information about the Python-ideas mailing list