Translate this to python?

Xavier Morel xavier.morel at masklinn.net
Fri Jan 6 11:28:22 EST 2006


Heiko Wundram wrote:
> Xavier Morel wrote:
>> I think that xrange is also soon-to-be deprecated (xrange eats a little
>> less memory and is slightly faster to _create_, but much slower to
>> _iterate over_ than range)
> 
> It might be slower to iterate using xrange, but xrange certainly has its
> place in Python... Try the following on your computer:
> 
> for x in range(10**10):
>     print x
> 
> for x in xrange(10**10):
>     print x
> 
> Tell me which one doesn't overflow your memory. ;-) And before you come
> telling me that these constraints are articial, I've _written_ programs
> that had to iterate over 2**24 (the set of 10.* IP addresses), and I most
> certainly wouldn't have wanted the machines to contain 384+ MB of RAM just
> to store the number objects that range creates.
> 
> --- Heiko.

While xrange does have it's place in Python, it has very few actual uses 
(yours being one of the few), and is usually more harmful than beneficial.

While the deprecation of xrange is not that "soon", it is part of the 
Python 3000 PEP (http://www.python.org/peps/pep-3000.html#id38) along 
with the deprecation of most FP-facilities of Python (filter, map, reduce).

It should also be noted that reimplementing xrange when needed is 
trivial and can be done with a 5-lines generator for the minimal version 
(1 argument, 0-n range) and probably less than 10 lines for the full 
blown version equivalent to the current one (including start, end and 
step arguments)



More information about the Python-list mailing list