l = range(int(1E9))

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun May 3 06:32:14 EDT 2015


On Sun, 3 May 2015 02:17 am, BartC wrote:

> But for looping over a simple integer range, then using 'range' to
> denote the range (and build a list as it used to do), was how it was
> done. And earlier on people would have been porting coding code to
> Python at which point a straightforward 'for i=a to b' loop suddenly
> acquired a substantial overhead it didn't have before!


The oldest version of Python that is documented on the python.org site is
1.4, and it had xrange:

https://docs.python.org/release/1.4/lib/node26.html

The oldest version of Python I have is Python 0.9.1, which has range but not
xrange:

steve at runes:~/personal/python/Interpreters/python-0.9.1$ ./python0.9.1
>>> range
<built-in function 'builtin.range'>
>>> xrange
Unhandled exception: undefined name: xrange
Stack backtrace (innermost last):
  File "<stdin>", line 1


but then Python 0.9 was missing a lot of features, such as double quoted
strings, and various operators:

>>> "a"
Parsing error: file <stdin>, line 1:
"a"
 ^
Unhandled exception: run-time error: syntax error
>>> 2**3
Parsing error: file <stdin>, line 1:
2**3
   ^
Unhandled exception: run-time error: syntax error


It's unfair to consider anything before version 1.0: it is obvious that
pre-1.0 Python was still incomplete and a work in progress.

If you check the versions here:

http://legacy.python.org/download/releases/src/

you will see that xrange was available in 1.1, but (probably) not in 1.0. So
xrange has been available since almost the start. By version 1.4, or
possibly 1.5, the general advice given as I remember it was that if the
number of loops was small, range was faster than xrange, but if it was
large, xrange used less memory.

On the basis of two software development principles:

(1) release early, release often;

(2) avoid premature optimization;

I don't think there is any problem with range being released before the need
for xrange was proven. Python, especially in the early days, was considered
more of a scripting language than a general purpose language, and scripting
languages often lack a C-style for-loop, using a foreach loop instead. E.g.
I believe the canonical way to loop in bash is something like:

    for $i in `seq start stop` do ... 

(by memory).


If we were creating Python from scratch now, we would never have had an
eager range() that returns a list. But back in the early 1990s, that wasn't
as obvious as it is in hindsight.



-- 
Steven




More information about the Python-list mailing list