PEP 276 Simple Iterator for ints (fwd)
James_Althoff at i2.com
James_Althoff at i2.com
Wed Nov 14 19:54:54 EST 2001
David Eppstein wrote:
>James_Althoff at i2.com wrote:
>> If not 3), then there is the issue of having to do an extra computation
for
>> the common case of indexing a sequence as in, e.g.,
>>
>> for i in 0 .. len(sequence)-1:
>
>This is very common in C, but is it so common in Python?
>Isn't it usually more idiomatic to do
> for item in sequence
>?
Yes, that is the common idiom.
PEP 276 is aimed at cases where "for item in sequence" doesn't work as in,
for example:
for rowcount in xrange(table.getRowCount()): # valid Python
print table.getValueAt(rowcount,0)
for rowcount in table.getRowCount(): # as proposed in PEP 276
print table.getValueAt(rowcount,0)
for rowcount in [0 .. table.getRowCount()-1]: # Haskell-esque syntax
print table.getValueAt(rowcount,0)
In this example:
for row in table.getRows():
doesn't work because there is no "table.getRows()" method that can be
called. Hence, the need for indexing.
As noted in the PEP, the commonly used (in Jython) Java/Swing/Jython class
DefaultTableModel is an example where there is a "getValueAt" method (that
requires a row index and a column index) but no "getRows" method.
Jim
More information about the Python-list
mailing list