Is current integer for-loop syntax a wart?
Michael Chermside
mcherm at destiny.com
Mon Apr 8 14:33:04 EDT 2002
> PEP 276 (Simple Iterator for ints) and PEP 284 (Integer for-loops), among
> others, are written from the perspective that the current integer for-loop
> syntax
>
> for i in range(10):
>
> is a wart. Is this true? That is, where does the Python community stand on
> this issue?
I think it's a wart.
Actually, I have only two complaints. The first is a minor, almost
conceptual point... just because I want to loop from 0 to 9 doesn't mean
I should have to allocate memory for the list [0,1,2,3,4,5,6,7,8,9]. The
objection is more meaningful for longer loops. Of course, it's not a
very strong objection, because I have options like:
for i in xrange(10):
... but if I'm a beginner I might not know about that. And sometimes
using range() is faster than using xrange() (I'm not clear on why). But
this is really NOT an important objection and the more I consider it the
less I care.
The second complain is significant (I think). That is the following:
for i in range(len(mySequence)):
This is something that I use all the time, and more relevently it is
something that BEGINNERS use all the time. I want Python to be REALLY
easy for beginners. I argue all the time that Python is better than Java
even on pure syntax. In java you have to do this all the time:
Iterator i = myCollection.iterator();
while( i.hasNext() ) {
MyClass x = (MyClass) i.next();
<code using x>
}
In python, that's as easy as this:
for x in myCollection:
<code using x>
or sometimes even this:
[<code using x> for x in myCollection]
This is a real win for Python... having to type all that boilerplate
every time I want to go through a list is painful... having syntax which
makes COMMON things EASY, and UNUSUAL things EXPLICIT is a very
important feature of Python.
Well, in my opinion, having to write
for i in range(len(myList)):
begins to smack of too much boilerplate. Frankly, I'd be satisfied with
option 2 from PEP 212 http://python.sourceforge.net/peps/pep-0212.html),
making it as simple as:
for i in indices(myList):
Or I'd be satisfied with PEP 276
(http://python.sourceforge.net/peps/pep-0276.html), although the fact
that it would affect contexs other than loops makes me somewhat queasy.
----
It has been suggested that the awkwardness in using range() or xrange()
for loops which are not half-open, or which have a step size other than
1 is a problem in need of a solution. I say "Wart?" -- ie, I've never
found it to be a problem myself. If I get confused while writing it, I
just find a way to be more explicit... often resorting to a "while" loop
instead of "for". Never thought it was any big deal.
---
Finally, the BDFL has proposed that release 2.3 avoid introducing new
syntax. GREAT IDEA. While I do claim to see some warts, I wouldn't say
that these are in any way a priority! Let's go for solidity first, and
language design second... because the language design is already excellent!
-- Michael Chermside
Final commen
More information about the Python-list
mailing list