[Python-Dev] Lockstep iteration - eureka!
Paul Prescod
paul@prescod.net
Thu, 17 Aug 2000 08:57:08 -0400
Tim Peters wrote:
>
> ...
> > If you want a more efficient way to do it, it's available (just not as
> > syntactically beautiful -- same as range/xrangel).
>
> Which way would that be? I don't know of one, "efficient" either in the
> sense of runtime speed or of directness of expression.
One of the reasons for adding range literals was for efficiency.
So
for x in [:len(seq)]:
...
should be efficient.
> The "loop index" isn't an accident of the way Python happens to implement
> "for" today, it's the very basis of Python's thing.__getitem__(i)/IndexError
> iteration protocol. Exposing it is natural, because *it* is natural.
I don't think of iterators as indexing in terms of numbers. Otherwise I
could do this:
>>> a={0:"zero",1:"one",2:"two",3:"three"}
>>> for i in a:
... print i
...
So from a Python user's point of view, for-looping has nothing to do
with integers. From a Python class/module creator's point of view it
does have to do with integers. I wouldn't be either surprised nor
disappointed if that changed one day.
> Sorry, but seq.keys() just makes me squirm. It's a little step down the
> Lispish path of making everything look the same. I don't want to see
> float.write() either <wink>.
You'll have to explain your squeamishness better if you expect us to
channel you in the future. Why do I use the same syntax for indexing
sequences and dictionaries and for deleting sequence and dictionary
items? Is the rule: "syntax can work across types but method names
should never be shared"?
--
Paul Prescod - Not encumbered by corporate consensus
Simplicity does not precede complexity, but follows it.
- http://www.cs.yale.edu/homes/perlis-alan/quotes.html