why no "do : until"?

David Morley morley at Borokay.ai.sri.com
Mon Jan 8 21:10:55 EST 2001


Peter Schneider-Kamp writes:

>   David Morley wrote:
>   > 
>   > What would be wrong with:
>   > 
>   > class Indexing:
>   >     def __init__(self, thing):
>   >         self.thing = thing
>   >     def __getitem__(self, index):
>   >         return (index, self.thing[index])
>   > 
>   > for (i,x) in Indexing(a):
>   >     ...
>
>   The efficiency. "for indexing" as proposed by Tim can
>   be implemented using the loop counter.

This also uses the loop counter rather than constructing an intermediate
list of indices.  There is the inefficiency of packing and unpacking the
(i,x) tuples, but I would hope that would be insignificant in Python.

Like Tim's suggestion, and unlike the standard "for i in range(len(a)):", it
can be used with objects whose length you don't know for sure (e.g., those
cute classes that iterate over the lines in a file reading in one line at a
time).  However this approach does not require any syntactic change to the
language.

David



More information about the Python-list mailing list