[Tutor] How to run this block of code dozens of times

Emile van Sebille emile at fenx.com
Mon Sep 17 17:23:36 CEST 2012

On 9/16/2012 8:17 PM Steven D'Aprano said...
> On 17/09/12 10:56, Scurvy Scott wrote:
>> Why would you use an underscore rather than a letter or name like I've
>> always seen. I've never seen an underscore used before.
> An underscore on its own is often used to mean "don't care". Like a
> scratch variable to hold a result when you don't actually need the result.
> So in a for-loop:
> for _ in range(20):
>      ...
> the idea is to tell the reader we don't actually care about the indexes
> 0, 1, 2, ... but only care that the loop happens 20 times.

Be sure you don't at some point depend on _ having a specific value 
however, as return values of functions are given the _ name in the 
absense of a designated label for the returned value:

ActivePython (ActiveState Software Inc.) based on
Python 2.6.6 (r266:84292, Aug 24 2010, 16:01:11) [MSC v.1500 32 bit 
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
 >>> def test(): return True
 >>> _
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
NameError: name '_' is not defined
 >>> test()
 >>> _


More information about the Tutor mailing list