1

Possible Duplicate:
Is it possible to implement a Python for range loop without an iterator variable?

just want to do something multiple times so I write

    for i in range(1, 10):
        print 'something'
        print 'do not use i'

so when I run PyLint it says : "Unused variable 'i'" of course. So what would be correct way to write this type of loop where I just want to loop multiple times.

    i = 0
    while i < 10:
        print 'test'
        i += 1

is this only way?

Community
  • 1
  • 1
Bojan Radojevic
  • 1,015
  • 3
  • 16
  • 26
  • Have a look at this question: http://stackoverflow.com/questions/818828/is-it-possible-to-implement-a-python-for-range-loop-without-an-iterator-variable – Nicolas Nov 11 '12 at 12:04
  • See this question: http://stackoverflow.com/questions/1885868/pythonic-way-to-ignore-for-loop-control-variable – AHM Nov 11 '12 at 12:04
  • 1
    Where does PEP8 say that you should not have loop variables you never read from? – ThiefMaster Nov 11 '12 at 12:06

3 Answers3

4

While you could substitute i with _, writing the loop as you have in the first place is perfectly fine, and writing it any other way would probably be considered strange to most Python programmers.

PyLint is checking too aggressively, and sometimes it should be ignored. Perhaps consider pyflakes if what you want is a checker that tells you if your code is likely to break; or pep8 if you want your style checked (variable naming etc).

joeln
  • 3,563
  • 25
  • 31
2

The for i in range(..) loop (use xrange() in python2 though) is the correct way to loop over a range of numbers in python. The pylint warning should be ignored in this case. However, in case of an unused variable it's often named _ to make it clearer that it's not used.

While there are other ways to loop those are less efficient since they usually involve function calls. Besides that doing so would just not be what people expect to see when reading your code.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
1

The version of PyLint I'm using right now oddly enough doesn't seem to cry about unused variables in this particular example. Be that as it may, there are several options:

  • Work with the iterator directly:

    i = iter(range(1, 10))
    
    try:
        while i.next():
            pass
    except StopIteration:
        pass  # safely ignore
    
  • Add a PyLint comment at the top of your file to disable the warning:

    # pylint: disable=X0123
    # NOTE: X0123 is just an example, I don't know the exact warning code
    
  • Potentially append an _ to the unused variable. I'm not sure whether or not this naming convention is respected, but it is mentioned in the official user manual:

    In our project we just accepted that we have to make some modifications in our code to please PyLint: stick to more naming conventions (unused variables ending in underscores, ...)

Filip Dupanović
  • 32,650
  • 13
  • 84
  • 114
  • 2
    Why not use `iter(range(1, 10))` or even better `xrange(1, 10)` instead of `range(1, 10).__iter__()`? – ThiefMaster Nov 11 '12 at 12:28
  • 3
    I think you should mention that option 1 should never be used in any real code where a normal loop can be used. – ThiefMaster Nov 11 '12 at 12:29
  • Option 1 is not equivalent to the original loop, because it breaks when the iterator returns a False value, and because it will swallow a `StopIteration` raised inside the loop body. A more faithful equivalent would be a `while True` whose condition is terminated with `try: i.next() except StopIteration: break`. – user4815162342 Nov 11 '12 at 12:47
  • I think the initial entry is already ugly enough, thank you @user4815162342. I'm being pragmatic knowing that the iterator won't return a `False` value and if your concerned about a `StopIterator` exception bubbling up in the block it means: 1) you didn't use any of the control flow tools, which handle the exception themselves, 2) you forgot to catch the exception therein and just done gone and shot yourself in the foot! – Filip Dupanović Nov 11 '12 at 13:06
  • It will work in most cases, yes, but sometimes bugs do happen, and those that manifest themselves by leaking `StopIteration` are not exactly unheard of. Pragmatically relying on the iterator never returning a False value creates code that is clever but brittle, but that's more of a difference in taste. – user4815162342 Nov 11 '12 at 14:47