Python hits the spot

Matt Gerrans mgerrans at mindspring.com
Mon Jun 24 07:23:35 CEST 2002


> "Matt Gerrans" <mgerrans at mindspring.com> writes:
> > > > for i in range(50*1000*1000):
> > > >    pass
> >
> > You call that a bug?   Did you consider trying xrange()?
>
> Maybe not a bug, but a pretty serious misfeature IMO.

It is neither a "misfeature" nor a bug, it is simply a matter of
understanding how to use your tools, as well as understanding how they work
and applying that knowledge.

I remember when I first read Learning Python and it said that range()
creates and returns a list.   It immediately occurred to me that if I was to
do a huge range that I would instead use a while loop with a counter;
shortly thereafter I was pleased to see xrange() introduced as the way to do
this.   In fact, one of my favorite aspects Python was that when I was
learning Python, I would be thinking, "it would be cool if it did blah" only
to turn the page and see that it does indeed -- this happened over and over
and continues to, as I learn more and as new releases come out.

Being the fearless maverick that I am, I just tried running this loop:

   for i in xrange(1000*1000*1000):  # The original loop X 200.
      pass

Guess what?   No memory problems whatsoever.   It took a few minutes (about
six and a half), but not much memory.







More information about the Python-list mailing list