Is current integer for-loop syntax a wart?

Skip Montanaro skip at pobox.com
Fri Mar 8 15:20:10 EST 2002


>>>>> "Jeremy" == Jeremy Cromwell <Cromwell> writes:

    Jeremy> [is] the current integer for-loop syntax

    Jeremy>     for i in range(10):

    Jeremy> a wart?

-Wart

Actually, I'd call it a mini-wart, sort of halfway between -Wart and -Wart?.

Explanation:

It seems a bit cumbersome that you have to call a function to use such a
fundamental control structure.  From an optimization point of view, a purely
syntactic indexing construct would allow better inference about data types.
You can't know ahead of time that range() always returns ints.  (I've never
seen anyone override it, but it *is* possible.)  Tools like PyChecker can
get away with assuming it's an int.  Psyco assumes builtin functions never
change, so it can dramtically improve for loop execution by dealing just
with C ints.  That means, strictly speaking, that it's not semantically
identical to CPython, however.  I believe over time it will be able to relax
its assumptions about range's befahvior and do the semantically right thing.
(I think Python2C also had an option that allowed the programmer to state
this explicitly.)

So, in my mind a syntactic replacement for range() would be nice, but not of
earth-shattering importance.

-- 
Skip Montanaro (skip at pobox.com - http://www.mojam.com/)




More information about the Python-list mailing list