PEP 284, Integer for-loops (fwd)
James_Althoff at i2.com
James_Althoff at i2.com
Mon Mar 11 14:45:42 EST 2002
[David Mertz]
|"Gnosis XML Utilities" has 3100 lines (written largely by me,
|with various contributors).
|There are *4* uses of [x]range() in 'for' loops. One is a test case
|that just uses 'range(500)' to do something "a bunch of times".
[James_Althoff at i2.com]
|I just did the same for a small section of our code. My quick little scan
|turned up 293 uses of [x]range.
|Our mileage might be different from others because our use of Jython is
for
|large-scale, shipping, production-level, commercial applications rather
|than for small, throw-away scripts or core-language libraries. Also (as
|mentioned) we use Jython and have to deal with tables
[David Mertz]
|My utility package is certainly not "throw-away scripts". And while
|it is generally tested with CPython, there's no reason it shouldn't
|work equally well with Jython. So some of that difference isn't
|really germane.
You are right. There are more categories and possible factors.
|The issue about iterating through data tables might be the key.
I think so too. What is key in particular is that the table components
were designed outside of Python and independent of Python's standard
conventions.
|But I think even then, my own style would generally not use range()
|iteration.
|
|For example, one might write (very simplified):
|
| rows = dbquery(sql)
| for i in range(len(rows)):
| print i, rows[i]
|
|But I would probably approach this more like:
|
| i = 0
| for row in dbquery(sql):
| print i, row
| i += 1
|
|It feels cleaner to me to iterate through the actual collection,
|rather than numerically over its length... even if I am then interested
|keeping track of the number.
That is nice alternative *when* you can say
row in table
But if your protocol is
def getRowCount(): pass
def getColumnCount(): pass
def getValueAt(rowIndex,columnIndex): pass
def setValueAt(rowIndex,columnIndex): pass
then that option is not available.
|If one fetches rows incrementally, I would
|still probably go for a 'while 1:' style.
That is an alternative. It seems unfortunate to me that one would have to
drop back to a "while 1:" style when dealing with something so common and
fundamental as indices. But that's just my opinion.
Jim Althoff
Senior Vice President
i2 Technologies, Inc.
More information about the Python-list
mailing list