Non-obvious name bindings
Ken Seehof
kseehof at neuralintegrator.com
Wed Nov 14 17:40:26 EST 2001
Jeff Shannon wrote:
> . . .
> If I'm reading the bug report right, then it *is* expected behavior that
list
> comp variables are function-local variables, not true "temporary"
variables.
>
> C++ (IIRC) treats loop indices as being limited to the scope of the loop
only,
> and invalid once the loop ends. Python, however, has a much simpler
scoping
> scheme, and doesn't use a separate scope for loops (and list comps are
just
> syntax sugar for a for-loop).
>
> . . .
> Jeff Shannon
> Technician/Programmer
> Credit International
Actually, the index has the scope that contains the 'for' statement, so
it does persist after the loop.
int main(int argc, char* argv[])
{
for (int i=0; i<10; i++)
{
printf("%d ", i);
}
printf("\n\n i = %d", i);
}
------------------------
0 1 2 3 4 5 6 7 8 9
i = 9
- Ken
More information about the Python-list
mailing list