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

Michael Charlton myaskovsky at hotmail.com
Wed Jul 28 09:11:00 EDT 2004


I must admit I've never heard of this before either but, I'm no world
authority on Python.

I've just tried it on some toy code and as always I run pychecker on it. So
with

            def spam():
                   for i in range(10):
                        print 'a'

I get "Local variable (i) not used". But with the following:

            def spam():
                   for _ in range(10):
                        print 'a'

I get no such warning. Is pychecker aware of this convention or is it
coincidental? I don't
know.

                                    Michael Charlton



"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.
>
> -- 
> David Eppstein
> Computer Science Dept., Univ. of California, Irvine
> http://www.ics.uci.edu/~eppstein/





More information about the Python-list mailing list