more fun with PEP 276

James_Althoff at James_Althoff at
Wed Dec 5 20:29:39 CET 2001

Jeff Shannon wrote:
>James_Althoff at wrote:
>> The (claimed) advantages with this scheme include:
>> - no syntax changes required (!!!)
>> - handles all combinations of closed/open intervals
>> - handles descending as well as ascending intervals
>> - allows step size to be specified
>> - reuses the "i in iterator" paradigm of existing for-loops
>> - supports shortcuts for the common case of indexing from 0 to len-1
>> - works outside of for-loops ("in" statement, list & tuple functions)
>> - no confusion with or overloading of list or tuple syntax
>> - no list versus iterator confusion
>> - is reasonably transparent (once you get used to it ;-)
>> - is straightforward to implement
>> On the down side:
>> - not as immediately transparent as "-5 <= i <= 5"
>Nor is it immediately more transparent, or shorter, or in much of any
other way
>(that I can see) preferable to  "range(10)"
>for i in 0 / span / len(mylist):
>#or even..
>for i in span/len(mylist):
>for i in range(len(mylist):
>Hmmm... what's the advantage again?  Seems like a lot of work for not much

To match

    for i in span / len(mylist):

more closely you would use xrange instead of range since range actualizes
the list before iterating the values (which, of course, might not matter in
many cases).  So the better comparison is to

    for i in xrange(len(mylist)):

Although the above is fine, there are a couple of things one can point out:

- xrange is not an ideal name for something used in such a common idiom.
- a nested function is not ideal for such a common idiom.
- the fact that one has the option of using either range or xrange causes
enough confusion that one observes inquiries and debates from time to time
(on this mailing list, for example) about best/proper usage of each.
- xrange and range are both oriented to intervals that are closed on the
left and open on the right.  If you want to specify an interval that is
open on the left and closed on the right, then you have to do extra
calculations that can be error-prone, e.g.,

    left = getLeftSide()
    right = getRightSide()

    for i in xrange(left+1,right+1):  # open on left, closed on right --
not so obvious

Compared to:

    for i in left / span // right:  # open on left, closed on right

Furthermore, there is no syntactic mechanism in xrange/range that visually
indicates the fact that the left side is closed and the right is open.  And
for any other combination (e.g., open on the left, closed on the right)
xrange and range aren't very obvious.

And using span -- in my experience with this exercise -- didn't seem like
"a lot of work".  To me, at least, it seemed quite easy.

So I would suggest that one key difference between span and xrange is that
span has a better mechanism for specifying all combinations of
"open/closed"-ness for intervals whereas xrange and range really target
"closed on left / open on right" intervals (which, granted, are very common
-- but not what one needs for all occasions).


More information about the Python-list mailing list