x[i] as loop variable

Raymond Hettinger vze4rx4y at verizon.net
Thu Oct 23 16:28:14 EDT 2003


[Gerrit Holl]
> PEP 289 says:
> (Loop variables may also use constructs like x[i] or x.a; this form may be
deprecated.)

That was taken out of the pep because it is a distractor issue.


> What is the use of this? When should I use it?

In general, it should never be used unless you're trying to obfuscate
your code.  It is a long standing, unintended by-product of the
grammar that any lvalue can be used as a loop variable.  Most folks
aren't aware of it and everyone is better off not knowing about it.
In fact, I wish it hadn't come-up at all because someone will read
about it and adopt the atrocity as their new favorite style.

So, close your eyes, forget this note, and go read about
something interesting and useful.


Raymond Hettinger







More information about the Python-list mailing list