Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it considered bad practice to re-use an iterating variable multiple times in a given script? [closed]

Tags:

python

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?

like image 640
Pietro Ferrero Avatar asked Jan 30 '14 17:01

Pietro Ferrero


4 Answers

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
like image 118
Cilyan Avatar answered Oct 05 '22 22:10

Cilyan


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:
    ...
like image 26
sateesh Avatar answered Oct 06 '22 00:10

sateesh


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.

like image 23
fedorSmirnov Avatar answered Oct 05 '22 23:10

fedorSmirnov


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

like image 26
jbh Avatar answered Oct 05 '22 22:10

jbh