Is current integer for-loop syntax a wart?

Huaiyu Zhu huaiyu at gauss.almadan.ibm.com
Fri Mar 8 19:29:02 EST 2002


- Not a wart:

  1. It is very clear what it does.
  2. range() can be used elsewhere.  No new syntaxes.
  3. In most situations you loop over list of items instead of counters.


- Wart:

  When you do need the index, neither of the following is very nice looking:

     for i, x in zip(range(len(a)), a):
         b[i] = x

     for i in range(len(a)):
         b[i] = a[i]


So the wart is the way indices are generated, not how the for-loop is
constructed.  I think the iter(n) proposal addresses the right problem, but
the solution is too broad: it produces iterators in many uninteded places.


Two ideas that I do like:

      for i, x in items(a):
          b[i] = x

      for i in indices(a):
          b[i] = a[i]

They address the right problems at minimum cost.  They can be extended to
xitems and xindici, of course.


Huaiyu



More information about the Python-list mailing list