On 02/05/2015 16:26, BartC wrote:
On 30/04/2015 18:20, Ben Finney wrote:
Jon Ribbens <jon+use...@unequivocal.co.uk> writes:
If you use xrange() instead of range() then you will get an iterator
which will return each of the numbers in turn without any need to
create an enormous list of all of them.
If you use Python 3 instead of the obsolescent Python 2, the ‘range’
callable has this sensible behaviour by default.
When I first looked at Python 20 or so years ago this seemed to be the
standard way of writing a for-loop:
for i in range(N):
....
I remember being completely astonished at the time that 'range' actually
created a list of values from 0 to N-1.
Python was already known to be slow yet it deliberately crippled itself
by using just about the slowest method imaginable of executing a simple
iterative loop? By first creating a list of a million objects!
And sometimes you weren't even interested in the values but just wanted
to execute something N times so it was a wasted effort.
That was eventually fixed with xrange, but why do it like that in the
first place?
(At the time, I was creating bytecode languages as part of the
applications I was writing. An empty for-loop executed just one bytecode
per iteration, and it was also the fastest bytecode instruction. An
empty for-loop executed three Python bytecodes per iteration last time I
looked. It seems that Python used to like making a rod for its own back.)
I first started maybe 14 years ago and the standard way of writing a for
loop was, and still is:-
for item in items:
When did this change, or has it always been this way and you were simply
using an idiom from other languages?
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
--
https://mail.python.org/mailman/listinfo/python-list