PEP 284, Integer for-loops

Greg Ewing greg at cosc.canterbury.ac.nz
Wed Mar 6 22:37:19 EST 2002


James_Althoff at i2.com wrote:
> 
> Will the syntax not work for
> instances of a subclass of int (now possible in Python 2.2), for example?

Subclasses should work too, I think.

> Or is it the intention that the construct is *not* limited strictly to the
> Python builtin classes "int" and "long"?  Will it work for any class that
> defines __lt__, __lg__, __gt__, __ge__, __add__, and __radd__ methods, for
> example?

No, I don't think so. It won't be possible to implement it
by just starting with the lower bound and repeatedly adding
1 or taking its successor or something. 

Firstly, the values bound to the index variable are
always *integers* (just like range() et al always
return sequences of integers). We're *not* intending
that, e.g. you should be able to use mxDateTime
objects for the bounds and get a range of times
one second apart or something!

Secondly, if the bounds are floats, the correct place 
to start is ceil(lowbound), not lowbound itself, or 
even int(lowbound).

So, floats will have to be treated as a special case,
and if we're having special cases, it would be better to
have *only* special cases -- i.e. restrict it to types
that we'res sure we know enough about to do the right
thing with. That means ints, longints and floats.

In any case, you're right that the PEP needs to nail
things down better in this area.

-- 
Greg Ewing, Computer Science Dept, University of Canterbury,	  
Christchurch, New Zealand
To get my email address, please visit my web page:	  
http://www.cosc.canterbury.ac.nz/~greg



More information about the Python-list mailing list