what does 'for _ in range()' mean?

Roy Smith roy at panix.com
Wed Jul 28 04:49:18 EDT 2004


In article <eppstein-5B9460.23395927072004 at news.service.uci.edu>,
 David Eppstein <eppstein at ics.uci.edu> wrote:

> In article <2moucmFo8dpdU1 at uni-berlin.de>,
>  Jon Perez <jbperez808 at wahoo.com> wrote:
> 
> > I saw this code snippet:
> > 
> > sock.listen(20)
> > for _ in range(20):
> >      newsock, client_addr = sock.accept()
> >      print "Client connected:", client_addr
> >      data[newsock] = ""
> > 
> > why use _ for this example?  Is there any
> > optimization to be had using it?
> > 
> > I know that in the interpreter _ means the
> > last value calculated, but what does _ mean
> > inside source code?
> 
> AFAIK it's just a variable like any other, but by convention it means 
> that you don't intend to use that value, just read it and ignore it.

I've never heard of that convention before.  Is it some python-specific 
thing, or is my ignorance more global in scope?

In any case, I'd vote for some more useful variable name.  In the above 
case, something like connectionNumber or whatever would be more 
self-explanitory.



More information about the Python-list mailing list