
Tim Peters wrote:
Scheme has no loops in Python's sense -- things like "do" are shorthand for expressing stylized recursion
But it does have foreach and map, which are the moral equivalent of Python's for-loops and list comprehensions. The body is a lambda which takes the loop variable as a parameter, thus providing the extra level of scope. Recursion isn't needed.
When people talk about giving a Python for-loop vrbl its own scope, they generally don't mean a new scope on _each iteration_,
But that's exactly what you *do* need in order for a for-loop with a lambda in it to behave intuitively. If that's not what people mean, it's because they don't fully understand what they really mean to mean. :-) BTW, I'm not suggesting that a new stack frame gets allocated for every iteration -- all you need is a cell.
about the same as creating a nested block with its own autos in C.
Analogies with C aren't very helpful here, because it doesn't have closures, so it's only a matter of visibility, not lifetime.
creating a new scope on each iteration sounds hard to explain in Python.
But is it harder to explain than the reason someone's loop-with-a-lambda doesn't do what they expect? BTW, I wouldn't explain it by saying it creates a new scope, I'd say it creates a new binding on each iteration, or something like that. In my earlier proposal, you would actually say that explicitly, with something like for new i in range(10): ...
Abusing the default-argument machinery to capture current bindings is never necessary, and _is_ abuse.
But it's abuse that still happens, because although scoping has been fixed, other parts of the story are still missing. -- Greg