[Tutor] dummy, underscore and unused local variables

Steven D'Aprano steve at pearwood.info
Sun Jun 12 02:02:15 CEST 2011


Tim Johnson wrote:
> Consider the following code:
> for i in range(mylimit):
>     foo()
> running pychecker gives me a 
> """
> Local variable (i) not used 
> """
> complaint.
> If I use 
> for dummy in range(mylimit):
>     ....
> ## or
> for _ in range(mylimit):
>     ....
> I get no complaint from pychecker.	
> I would welcome comments on best practices for this issue.


The pychecker warning is just advisory. You can ignore it if you like. 
But using "dummy" or "_" a variable name you don't care about is good 
practice, as it tells the reader that they don't need to care about that 
variable.


> On a related note: from the python interpreter if I do
>>>> help(_) 
> I get 
> Help on bool object:


This is a side-effect of a feature used in the Python interpreter. _ is 
used to hold the last result:


 >>> 1 + 1
2
 >>> _
2
 >>> ['x', 3] + [None, 4]
['x', 3, None, 4]
 >>> _
['x', 3, None, 4]


So it's just a coincidence that when you called help(_) the previous 
command happened to result in True or False. If you try again now, 
you'll probably get something completely different.





-- 
Steven



More information about the Tutor mailing list