Why I think range is a wart.

Gonçalo Rodrigues op73418 at mail.telepac.pt
Wed Mar 13 10:47:27 EST 2002


Some tens (or hundreds - I lost count already) of posts ago, someone
asked what was the wartiness of the range (and by extension, of xrange)
builtin. The question sunked in my brain, and last night it resurfaced
back with an accompanying answer. Actually, the title is just a bait: it
should be named, Why I think the use I make of range is a wart.

For starters, range (and xrange) is a perfectly sensible, reasonable and
useful buit-in. But the biggest use I make of it is in iterating through
a list, e.g. as in

for index in range(len(mylist)):
    <whatever>

and in the <whatever> body I need to make use of the indexes. I am not
sufficiently qualified to say if this is Pythonic or not, but I find
myself doing this quite often.

Now, a list is not just a pile of objects, it is a pile of objects plus
an implict order on them. In more mathematical terms, it is an ordinal,
a set with a total order in it. And in the above loop, I am using range
to get at this order, in other words, I am not using all the information
that is implicit in the mylist object, quite the opposite, I am doubling
it.

So, syntactic issues aside - btw I was a fan of PEP 276, oh well... -
this, for me, is the wartiness of range.

These are just my two cents on it. Feel free to bash and knock down my
comments.

Gonçalo Rodrigues

P.S: PEP 279 proposes adding indexed to generators which would "clean"
the above wart.




More information about the Python-list mailing list