for loop without variable

Tim Chase python.list at tim.thechases.com
Wed Jan 9 18:00:53 EST 2008


>> Hi. I'd like to be able to write a loop such as:
>> for i in range(10):
>>     pass
>> but without the i variable. The reason for this is I'm using pylint
>> and it complains about the unused variable i.
> 
> if a computer tells you to do something stupid, it's often better to 
> find a way to tell the computer to shut up than to actually do some-
> thing stupid.
> 
> (pylint's online documentation is remarkably unreadable, so I cannot 
> help you with the details, but surely there's a way to disable that 
> test, either globally, or for a specific region of code?).

That documentation on PyLint[1] is atrocious!

 From my reading of it, at the beginning of your file, you put a 
comment something like

   # pylint: disable-msg=W0612

which should disable that particular message.  Totally untested.

I don't know if PyLint is smart enough, but if you don't use "i", 
you might use the common "throwaway" variable of "_":

   for _ in xrange(10):
     do_something()

in which case it _might_ recognize that it's a throwaway variable 
and not warn you about it.  At least that would be nice of it. 
But given the abusive nature of the documenation, I'm not sure 
that's the case ;)

-tkc
[1]http://www.logilab.org/card/pylintfeatures





More information about the Python-list mailing list