PEP 276 Simple Iterator for ints

Skip Montanaro skip at pobox.com
Thu Nov 15 11:32:01 CET 2001


    Jim> Getting back to the motivating example of PEP 276 -- as stated in
    Jim> the PEP, adding an iterator to types.IntType would allow one to
    Jim> write:

    Jim> for rowcount in table.getRowCount():

    Jim> Summarizing some of the other possibilities, one might write:
    ...
    Jim> for rowcount in range(table.getRowCount()):
    Jim> for rowcount in xrange(table.getRowCount()):

No need to even mention these two dinosaurs... ;-)

    Jim> for rowcount in [0 .. table.getRowCount()-1]:

I think I would still write the above as

    for rowcount in [0, step, ... table.getRowCount()]:

That would get you the sequence

    0, 1*step, 2*step, ... (table.getRowCount()-1)*step

and is more flexible than your proposal.  There's no reason you couldn't
have

    for f in [0.0, 1.0, ... sys.maxint]:

to enumerate floats up to but not including sys.maxint (care needs to be
taken to not to compound errors).

You could also build character sequences I think:

    for c in ["a", "b", ... "z"]:

which would use ord() and chr() internally.  (You could probably extend to
multi-character strings, but that would raise locale and string vs. unicode
issues.)






More information about the Python-list mailing list