[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Can I change the for iteraotr variable in a loop?
- From: Roberto Ierusalimschy <roberto@...>
- Date: Wed, 16 May 2012 10:09:14 -0300
> hi list and Roberto :-)
>
> the PiL says you should never change the for iteraotr variable, but I
> can not found this in reference, so I want to ask whether this
> variable is just a normal variable? that is, if I change its value,
> except the value of iterator will reset before the next loop is
> executed, what else behavior will happen? or is this a undefined
> hebahvior?
>
> regards,
> Xavier
If PiL does not agree with the manual, follow the manual.
According to the manual, the visible iterator variable is a regular
local variable that gets a copy of the "real" iterator variable; so, you
may assign to it, but that assignment does not affect the loop.
Nevertheless, PiL is partially correct in that it is not a good practice
to rely on this specific behavior. Certainly other people reading your
code (or even you after some time) will have doubts about what is going
on.
-- Roberto