Speed-up for loops
roy at panix.com
Tue Sep 7 14:09:12 CEST 2010
In article <ny3ho.32445$la2.18202 at newsfe23.ams2>,
"BartC" <bartc at freeuk.com> wrote:
> (BTW why doesn't Python 3 just accept 'xrange' as a
> synonym for 'range'?)
If you've ever tried to maintain a legacy code base, you'll understand
why "there's only one way to do it" is A Good Thing.
Imagine that you're looking at some code which was written years ago, by
people who are no longer around to answer questions. In one place, you
for i in range(n):
and in another, you see:
for j in xrange(n):
If you are truly a Python expert, you'll say to yourself, "range and
xrange are synonyms", and move on to other things. If, however, you're
not really an expert, you'll look at this and say, "Hmmm, in one place
they used range(), and in another they used xrange(). Clearly, there's
some reason for the difference, I need to figure out what it is, because
that's probably key to my understanding why this code isn't working".
So, you spend the next two hours pouring over reference manuals trying
to understand the subtle difference, until your friend comes over and
says, "You dolt, you just wasted half the afternoon. They're the same
thing. Move on, this is not the bug you're looking for".
If you think stuff like that can't happen, you've probably never jumped
into the middle of a legacy code base written in a language you don't
know very well.
More information about the Python-list