Many languages have a for-each loop. In some, like Python, it’s the only kind of for loop:

for i in range(10): print(i) In most languages, the loop variable is only in scope within the code controlled by the for loop,[1] except in languages that don’t have granular scopes at all, like Python.[2]

So, is that i a variable that gets updated each time through the loop or is it a new constant that gets defined each time through the loop?

Almost every language treats it as a reused variable. Swift, and C# since version 5.0, treat it as separate constants. I think Swift is right here (and C# was right to change, despite the backward-compatibility pain), and everyone else is wrong.

However, if there is a major language that should be using the traditional behavior, it’s Python.
4

When the first betas for Swift came out, I was impressed by their collection design. In particular, the way it allows them to write map-style functions that are lazy (like Python 3), but still as full-featured as possible. On further reflection, I realized that you can get the same kinds of views without needing Swift's complicated idea of generalized indexes. So, as soon as I had some spare time, I wrote up an implementation.
2

In a previous post, I explained in detail how lookup works in Python. But, briefly, Python has certain optimizations baked into the design; those optimizations may sometimes restrict your code (e.g., you can't use exec to set a local variable), and may even restrict other optimizations the implementation might want to do. So, let's go back to the start and redesign the lookup model and see if there's an alternative.
2
Blog Archive
About Me
About Me
Loading
Dynamic Views theme. Powered by Blogger. Report Abuse.