python loops

Nicko usenet at nicko.org
Sun Sep 3 05:43:41 EDT 2006


Fredrik Lundh wrote:
> Nicko wrote:
>
> > ... In the case of the idiom "for i in
> > range(x):..." there absolutely no utility whatsoever in creating and
> > recording the list of objects.
>
> for short lists, both objects create the *same* number of objects.

This is true for long lists too, if you iterate over the full range,
but what I wrote was "creating and recording". The range() function
generates a variable-sized, potentially large object and retains all of
the items in the range while xrange() generates a fairly small, fixed
sized object and only hangs on to one item at a time.  Furthermore,
it's not at all uncommon for loops to be terminated early. With range()
you incur the cost of creating all the objects, and a list large enough
to hold them, irrespective of if you are going to use them.

> if you cannot refrain from pulling arguments out of your ass, you not
> really the right person to talk about hygiene.

I'm impressed but your mature argument. Clearly, in the face of such
compelling reasoning, I shall have to concede that we should all
generate our range lists up front.

Nicko




More information about the Python-list mailing list