8

Is it considered bad practice to re-use the same iterating variable name throughout multiple for-loops in a given script?

For example,

for url in urls1:
    print url

for url in urls2:
    print url

for url in urls3:
    print url

I know that the url variable isn't limited in scope to the for loop, so there's potential while using the url variable outside of the for loop for it to get messy and may be more difficult to understand. But I'm curious, is there a "best" practice? Should I be using conventions like "url1", "url2"? Or am I overthinking this and it's just whatever works to make it easier to understand?

Garrett
  • 4,007
  • 2
  • 41
  • 59
Pietro Ferrero
  • 476
  • 2
  • 10
  • 1
    There's nothing wrong with doing that - the only problem could occur if you wanted to use the last item `url` from loop one somewhere after loop two, but you seem to understand this anyway. – danodonovan Jan 30 '14 at 17:10
  • I think it's down to opinion but I personally do it. I code in Delphi right now so you have to explicitly declare variables at the start of a procedure so it just gets messy if you use different ones for each loop. – OGHaza Jan 30 '14 at 17:13
  • It's fine, as long as you know what you're doing. – wim Jan 30 '14 at 17:17

4 Answers4

4

A good variable name is a one that helps understand the program (and doesn't conflict with a reserved or well-known name). The answers will probably be only subjective, but I'd say that as long as the variable name you use mean the same thing in all for loops, that's ok.

The scope of the variable is indeed not limited to the for loop but the value will be overwritten when doing the first iteration of the next for loop. On the opposite, such a code would be nasty:

for url1 in urls1:
    print url1
for url2 in urls2:
    print url1, url2
Cilyan
  • 7,883
  • 1
  • 29
  • 37
2

I think it all depends on the context. For example in your case one could have written it as :

for url in urls1+urls2+urls3:
    print url

But your example is for the purpose of the demonstration. If it is just a loop variable that you want to use for iteration I don't see any problem in reusing the same loop variable multiple times. On the other hand if it makes sense, you could also choose a context sensitive name for each iteration. For example:

for base_url in urls:
    ....

for dev_url in urls:
    ...
sateesh
  • 27,947
  • 7
  • 36
  • 45
  • 2
    Your first example is not exactly the same as you first build a bigger list first. You could use `for url in itertools.chain(urls1, urls2, urls3)` but that's not that nice finally. ;) – Cilyan Jan 30 '14 at 17:21
1

If you have really many lines of code inside your loops where you do many operations on your iterations variable, it might be a good idea to give it an unique, meaningful name, especially if the different loops iterate over different kinds of objects.

In other cases, like in the example you provided, the iteration variable in the different loops represents the same kind of object and giving it the same name improves the code readability.

So all in all, you have to find a way that makes your code most readable.

fedorSmirnov
  • 701
  • 3
  • 9
  • 19
1

The variable should lend itself to the readability of the code. This code will all run fine regardless of the variable name. I do want to point out an edge case that I run into from time to time, strictly in python 2.7.

Using list comprehension in python 2.7 leaks the target variable name. For Example

x = 'before'
a = [x for x in 1, 2, 3]
print x # this prints '3', not 'before'

I am guilty of doing things such as this, so i try to keep variables unique. This bug/feature was fixed/changed in python3.

Reference

Community
  • 1
  • 1
jbh
  • 1,153
  • 2
  • 11
  • 24