8

If I need a for loop in Python:

for i in range(1,42):
    print "spam"

but don't use the i for anything, pylint complains about the unused variable. How should I handle this? I know you can do this:

for dummy_index in range(1,42):
    print "spam"

but doing this seems quite strange to me. Is there a better way?

I'm quite new to Python, so forgive me if I'm missing something obvious.

aschultz
  • 1,658
  • 3
  • 20
  • 30
Jacxel
  • 1,634
  • 8
  • 22
  • 33
  • 1
    This is highly subjective. Either use a `_` or `dummy` prefix, or decide on a different scheme and set PyLint's `--dummy-variables-rgx` option accordingly (e.g., `unused_`). – Ferdinand Beyer Apr 12 '12 at 11:00
  • 1
    Actually, since "i" is used, and it is a necessary part of the syntax in the `for` statement, the only _dummy_ thing around is pylint itself. I'd advise placing this as a bug report in the pylint project. – jsbueno Apr 12 '12 at 13:32

5 Answers5

16
for _ in range(1,42):
    print "spam"
jamylak
  • 128,818
  • 30
  • 231
  • 230
  • Thought it might recognise it since it is traditionally used when you have no use of the variable. – jamylak Apr 12 '12 at 10:55
  • 7
    +1: With default settings, PyLint does not complain about unused variables starting with "dummy" or "_". Using "\_" to store unneeded values is common in Python (e.g. `foo, _ = func_returning_tuple()` Having said this, I would prefer `_i` over plain `_`. – Ferdinand Beyer Apr 12 '12 at 10:57
  • 6
    Beware: using `_` for this when also using the [`gettext`](http://docs.python.org/library/gettext.html) library (or thinking you might use it in the future) will cause problems. Gettext uses `_` as a i18n translating function (`_("Translate this text")`), but after the above loop `_` would suddenly be 41 instead and the next invocation would raise an exception. – Lauritz V. Thaulow Apr 12 '12 at 11:09
  • This is what i was doing, but i was hoping someone would point me in some direction where i didn't have to declare the unused variable, rather than just ignoring it. – Jacxel Apr 12 '12 at 11:12
  • 4
    @Jacxel: There is no "natural" way to loop *n* times without a counter variable in Python, and you should not resort to ugly hacks around it. Either just ignore the PyLint warning, configure it to ignore unused variables named `i`, or use a prefix, probably the default `_` (less distracting than `dummy`). – Ferdinand Beyer Apr 12 '12 at 11:43
9

There is no "natural" way to loop n times without a counter variable in Python, and you should not resort to ugly hacks just to silence code analyzers.

In your case I would suggest one of the following:

  • Just ignore the PyLint warning (or filter reported warnings for one-character variables)
  • Configure PyLint to ignore variables named i, that are usually only used in for loops anyway.
  • Mark unused variables using a prefix, probably using the default _ (it's less distracting than dummy)
Ferdinand Beyer
  • 64,979
  • 15
  • 154
  • 145
8

According to pylint documentation:

--dummy-variables-rgx=
          A regular expression matching names used for dummy variables (i.e.
          not used). [current: _|dummy]

In other words, if the name of the variable starts with an underscore, or with the letters dummy, pylint would not complain about the variable being unused:

for dummy in range(1, 42):
    print "spam"
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • 1
    i gave that solution in my question, im asking if there is a way to do it without ever declaring the variable – Jacxel Apr 12 '12 at 11:14
1

Usually you can work around it, just like this in your case:

>>> print "spam\n"*len(range(1,42))
luke14free
  • 2,529
  • 1
  • 17
  • 25
  • 2
    But probably the OP wants to do something more complex, with this being a simple example... – Paul Hiemstra Apr 12 '12 at 11:00
  • @PaulHiemstra what I'm saying is that probably even more complex things can be done without the unused variables. I can't imagine settings which cannot be solved without using dummy variables – luke14free Apr 12 '12 at 11:02
  • while there probably is a workaround such as this, im trying to make as few changes to the code as possible. im working on getting an existing framework up to pep8 and want to avoid making big changes where unnecessary. – Jacxel Apr 12 '12 at 11:16
  • @luke14free, I understand, and I agree – Paul Hiemstra Apr 12 '12 at 13:25
0

3 Simple Reasons

  1. There is no way to loop through your program without using a counter variable in a for loop.
  2. But you can create a program that goes from index[1] to index[2] just by adding if index[1]is done. return index[]+1.
  3. Unfortunately, you need to create an extra program which is not as efficient as the for loop and is not efficient in long programs.
drewteriyaki
  • 320
  • 1
  • 3
  • 12