for a in range(1000000) :
mcherm at mcherm.com
Mon Jun 16 20:01:11 CEST 2003
[... notes that "for a in range(x)" idiom seems wasteful of memory ...]
[... observes that an iterator-like construct would not waste the memory ...]
> Exist someting like this in python library? Why not all the people use
> this form an not range() that allocate memory? Maybe may questions are
> stupid but i'm a newbie in python, sorry.
Actually, you're really onto something here. The range() function is
a very old bit of Python, dating back YEARS before iterators even existed.
The xrange() function was created for exactly this purpose. Unfortunately,
the range() function could not simply be changed to work the way xrange()
works now, because there was lots of existing code out there that assumed
that range() returned a list. You see, the maintainers of Python work
quite hard to avoid as much backward-incompatibility as possible.
However, Guido has explicitly NOT tried to promote the use of xrange().
There have been versions of Python for which xrange() saved memory but
ran slower, but that was never the real reason (and in the newest versions
of Python xrange() is probably faster). The real reason is that Guido
doesn't want people to have to remember to use range() some places and
xrange() other places. He'd rather have people write "list(range(x))"
when they need to get a list, otherwise *never* use the version that
wastes memory. So eventually, (perhaps Python 3.0?) the plan is to migrate
to new behavior for range() and then get rid of xrange().
The issues that you're finding here are ones which are of current
concern to the Python developers. Look, for instance at this thread:
where this very issue was discussed on Python-dev this week. If you're
coming up with the same concerns and similar sorts of solutions to
what the Python developers are working on, then clearly you are not
asking "stupid" questions. Keep on asking them! And meanwhile, use
xrange() if you really need to save the memory and speed, or range()
if the performance doesn't hurt and you care more about readability.
-- Michael Chermside
More information about the Python-list