On 13/01/24 3:14 pm, Chris Angelico wrote:
On Sat, 13 Jan 2024 at 13:11, Left Right via Python-list
<python-list@python.org> wrote:
Very few
languages allow arbitrary complex expressions in the same place they
allow variable introduction.
What do you mean by this? Most languages I've worked with allow
variables to be initialized with arbitrary expressions, and a lot of
languages allow narrowly-scoped variables.
I think he means that in some languages the for-loop target serves as
the declaration of a new variable, and as such has to be a bare name.
Python isn't like that -- the target of a for-statement is treated
exactly the same way as the lhs of an assignment. It's not scoped to the
loop.
BTW, the equivalent thing is valid in C too, so anyone familiar with C
is unlikely to be surprised by this either.
#include <stdio.h>
int x[10];
int i;
int main() {
i = 5;
for (x[i] = 0; x[i] < 10; x[i]++)
printf("%d\n", x[i]);
}
Output:
0
1
2
3
4
5
6
7
8
9
--
Greg
--
https://mail.python.org/mailman/listinfo/python-list