PEP 284, Integer for-loops (fwd)

James_Althoff at i2.com James_Althoff at i2.com
Tue Mar 12 15:26:36 EST 2002


[Tim Peters]
> I didn't have subclassing in mind; e.g.,

Got it.  Wrapping is better, too, because one generic wrapper can handle
all table-like structures (instead of needing multiple subclasses).

[snip generously provided sample implementation]

> That implements more than the example at the start actually needs;
> cut back to taste <wink>.

Au contraire.  Why not forget simplicity and pour on the spices --
heartburn and high blood pressure be damned?  With this nice framework I
can throw in lots of recent suggestions along with the kitchen sink.

    def rowIndices() # return iterator of 0,1,...,r-1 (might not need it,
but I like it ;-)
    def colIndices() # similar
    def indexedRows() # return iterator of index,row pairs (might actually
be handy)
    def indexedCols() # similar

Yes, I just might go hog wild.

[snip]

> I'd try a dict, mapping types to unbound methods, like:
>
> rowcounter = {JTable: JTable.getRowCount,
>               DefaultTableModel: DefaultTableModel.getRowCount,
>               ThirdPartyBozo: ThirdPartyBozo.getNumberOfRows}
>
> def ri(table):
>     "Return indices of table rows."
>     return range(rowcounter[type(table)](table))
>
> I don't know whether that works exactly as-is in Jython, but you get
> the idea.

The JTable.getRowCount part works in Jython, but type(table) doesn't
because "type" returns the generic Jython class PyInstance -- kind of like
how cPython returns 'instance' for classic-class objects -- so it doesn't
differentiate properly.  But table.__class__ works so I can use that as an
alternative.

> Unless you have an unbounded number of classes (a bad Smalltalk
> habit you should have given up by now <wink>),

Actually, many implementations (due to the use of tagged pointers) have an
upper limit of 2 ** 31 classes -- I've rarely had the need to define more
than 2 ** 29 (give or take a couple that are often not *strictly* necessary
but thrown in for the sake of "purity over practicality") even in large
applications.  So "unbounded" really is a bit of an unfair
characterization.

Jim


oh yeah, forgot the <wink>.  <wink>






More information about the Python-list mailing list