Recap: PEP 276 Simple Iterator for ints

M.-A. Lemburg mal at lemburg.com
Fri Nov 16 17:12:18 EST 2001


James_Althoff at i2.com wrote:
> 
> While there is certainly merit and potential benefit in trying to invent a
> new, general purpose mechanism for specifying lists of numbers, it should
> be noted that for the problem that PEP 276 addresses, the various proposals
> above yield things like:
> 
>     for i in [0, 1 .. table.getRowCount()-1]:
>         for j in [0, 1 .. table.getColumnCount()-1]:
>             print table.getValueAt(i,j)
> 
>     for i in int[0:table.getRowCount()]:
>         for j in int[0:table.getColumnCount()]:
>             print table.getValueAt(i,j)
> 
>     for i in integers[0:table.getRowCount()]:
>         for j in integers[0:table.getRowCount()]:
>             print table.getValueAt(i,j)
> 
> (Acknowledging that not every proposed variation is shown here nor various
> proposed shortcuts for various proposals).
> 
> While acknowledging that several of these might read somewhat better than
> xrange/range in the scenario above, it is not universally apparent that
> they are a vast improvement (in this context). 

Well, depends on how you look at it: the third "proposal" is
already available in Python 2.2: you only have to write a
class exposing the iterator interface and then create a singleton
named "integers". No need to change the language: iterator
protocol, slices, ellipses -- it's all there.

> And for each such proposal,
> there is the issue as to whether the proposed new form returns an
> actualized list or an iterator and how well suited the given choice might
> be in various usage scenarios (given that you have to pick one or the
> other: actualized list or iterator).

Since for-loops always use iterators there's no problem here:
even if one of the constructs does not return an iterator
compatible sequence, the for-loop will wrap this object using a
generic sequence iterator.

In the "integer[slice]" case, an iterator should be returned which
can then be turned into a list or tuple by applying tuple()
or list() to it.

> Nonetheless, as stated several times before, PEP 276 does not preclude the
> adoption of a new mechanism for specifying lists of numbers similar to any
> of the above proposals -- it doesn't have to be an "either or" choice.  It
> simply provides a convenient way to access indexed structures in a
> for-loop.

I suppose we could make iterators compatible to lists
and tuples in most cases. Guido already has code
for this in place in list() and tuple() (but not list 
assignments), so you don't lose much buy only providing
an iterator.

-- 
Marc-Andre Lemburg
CEO eGenix.com Software GmbH
______________________________________________________________________
Consulting & Company:                           http://www.egenix.com/
Python Software:                        http://www.lemburg.com/python/





More information about the Python-list mailing list