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

John Roth newsgroups at jhrothjr.com
Wed Jul 28 06:28:33 EDT 2004


"David Eppstein" <eppstein at ics.uci.edu> wrote in message
news:eppstein-5B9460.23395927072004 at news.service.uci.edu...
> 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.

What convention? I have to agree with a couple
of other posters; I've never heard of it before.

If it really is a convention, it would be nice to have it
documented somewhere (like the single underscore
in front of a variable means "weak internal use").
Somewhere is most likely PEP 8 - the Python
Style Guide.

John Roth

>
> -- 
> David Eppstein
> Computer Science Dept., Univ. of California, Irvine
> http://www.ics.uci.edu/~eppstein/





More information about the Python-list mailing list