Range Operation pre-PEP

Roman Suzi rnd at onego.ru
Tue May 8 03:44:20 EDT 2001


Hello!

What is below is a half-baked proposal for new built-in
Python operation. If anybody wants to raise this flag 
again and fill gaps, please do so.

(I have not studied PEP howto much, so probably I missed
something important.)

I hope the idea of ".." is quite simple: make special syntactic 
form for xrange (range). I am not into C to reference-implement this
feature, so if anybody could do it...

All in all, I think the feature is most wanted/useful by/for beginners.

---------------------------

PEP: ???
Title: Range Operation
Version: $Revision: 1.0 $
Author: rnd at onego.ru (Roman Souzi), derived from thomas at xs4all.net (Thomas 
  Wouters)'s rejected PEP 204
Status: 
Type: Standards Track
Python-Version: 2.0
Created: 
Post-History:

Introduction

    This PEP describes the `range operation' proposal for Python 2.?.
    This PEP tracks the status and ownership of this feature, slated
    for introduction in Python 2.?.  It contains a description of the
    feature and outlines changes necessary to support the feature.
    This PEP summarizes discussions held in mailing list forums.

    The feature is needed in Python because it allows beginners 
    to learn for loop before learning range/xrange functions
    and adds more clarity to the program because of similarity 
    with mathematical notation.    

List ranges

    Ranges are sequences of numbers of a fixed stepping, often used in
    for-loops.  The Python for-loop is designed to iterate over a
    sequence directly:
    
        >>> l = ['a', 'b', 'c', 'd']
        >>> for item in l:
        ...     print item
        a
        b
        c
        d
    
    However, this solution is not always prudent.  Firstly, problems
    arise when altering the sequence in the body of the for-loop,
    resulting in the for-loop skipping items.  Secondly, it is not
    possible to iterate over, say, every second element of the
    sequence.  And thirdly, it is sometimes necessary to process an
    element based on its index, which is not readily available in the
    above construct.
    
    For these instances, and others where a range of numbers is
    desired, Python provides the `range' builtin function, which
    creates a list of numbers.  The `range' function takes three
    arguments, `start', `end' and `step'.  `start' and `step' are
    optional, and default to 0 and 1, respectively.
    
    The `range' function creates a list of numbers, starting at
    `start', with a step of `step', up to, but not including `end', so
    that `range(10)' produces a list that has exactly 10 items, the
    numbers 0 through 9.
    
    Using the `range' function, the above example would look like
    this:
    
        >>> for i in range(len(l)):
        ...     print l[i]
        a
        b
        c
        d
    
    Or, to start at the second element of `l' and processing only
    every second element from then on:
    
        >>> for i in range(1, len(l), 2):
        ...     print l[i]
        b
        d
    
    There are disadvantages with this approach:
    
    - Clarity of notation: beginners need to remember function name,
    its difference from xrange while other languages use syntactical
    construct for the same purpose, not a function.

    - what else?

The Proposed Solution

    The proposed implementation uses new syntactic 
    entity to specify range operation, as shown below:

        >>> for i in 1 .. 5:
        ...     print i
        1
        2
        3
        4
        5
 
    Or in extended form to specify a step:

        >>> for i in (1, 3) .. 5:
        ...     print i
        1
        3
        5
    
    The new operation ".." generates xrange object
    on by following rule:

        a1 .. an

        (a1, a2) .. an         
        
        is equivalent to:
         
        xrange(a1, an+1[, a2-a1])
        
        or other analog of xrange is that is applicable to the 
        type.
    
    N.B. Operation ".." deviates from slice notations.
    This is so in accordance with mathematical notation
    of ranges.

    There are no implicit forms of "..", that is, a1 and an must
    be present.

    To overload ".." operation in user-defined classes, 
    specific type must implement method
    __range__(self, a1, an, a2=1). This method must return either 
    iterator object or a list.

    In the table of operation priorities, .. operation must have less 
    (or equal) priority than lambda but more priority than 
    comma-separated display. 

    The implementation of range-literals will need changes 
    backward-compatible changes to built-in xrange() function
    to allow calling __range__ if the type is not a built-in
    object.  (or is it already done?)

    Otherwise, ".." is syntactical "sugar" for xrange().
      
Reference Implementation

    ".." is a binary operation which if the first argument is 2-tuple,
    uses xrange() with 3 arguments (as shown above) and if it is 
    a 1-tuple or other object, then it evaluates xrange with two
    arguments (step=1).

     TODO. Anybody?

Open issues

    - I'd liked to see  1, 3 .. n syntax instead of (1, 3) .. n, but
      the later will interfere with comma-separated lists.

    - How does will couple with iterators?

   ???

Copyright

    This document has been based on the PEP 204 and is also
    placed in the Public Domain.


------------------------

Sincerely yours, Roman A.Suzi
-- 
 - Petrozavodsk - Karelia - Russia - mailto:rnd at onego.ru -
 






More information about the Python-list mailing list