for loop without variable
Mike Meyer
mwm-keyword-python.b4bdba at mired.org
Thu Jan 10 14:50:24 EST 2008
On Thu, 10 Jan 2008 08:42:16 +0100 Hrvoje Niksic <hniksic at xemacs.org> wrote:
> Mike Meyer <mwm-keyword-python.b4bdba at mired.org> writes:
> > It sounds to me like your counter variable actually has meaning,
> It depends how the code is written. In the example such as:
>
> for meaningless_variable in xrange(number_of_attempts):
> ...
>
> the loop variable really has no meaning. Rewriting this code only to
> appease pylint is exactly that, it has nothing with making the code
> more readable.
Except in this case, the variable *has* a meaning. You've just chosen
to obfuscate it.
> > you've hidden that meaning by giving it the meaningless name "i". If
> > you give it a meaningful name, then there's an obvious way to do it
> > (which you listed yourself):
> >
> > while retries_left:
> [...]
>
> This loop contains more code and hence more opportunities for
> introducing bugs. For example, if you use "continue" anywhere in the
> loop, you will do one retry too much.
All correct - and I'm a big fan of minimizing code, as code you don't
write has no bugs in it. But you can still show the meaning of this
"meaningless" variable:
for number_of_attempts in xrange(maximum_attempts):
Of course, the OP's request is a better solution: since he doesn't
actually need the variable, removing it completely means there's one
less variable, which is one less thing you can set to the wrong value.
<mike
--
Mike Meyer <mwm at mired.org> http://www.mired.org/consulting.html
Independent Network/Unix/Perforce consultant, email for more information.
More information about the Python-list
mailing list