PEP 284, Integer for-loops

Carel Fellinger cfelling at iae.nl
Wed Mar 6 16:51:53 EST 2002


David Eppstein <eppstein at ics.uci.edu> wrote:
...

I haven't studied your proposal closely yet, so I'm probably way off
here, but...

> Issues

>     The following issues were raised in discussion of this and related
>     proposals on the Python list.
...
>     - Should types other than int, long, and float be allowed as
>       bounds?  Another choice would be to convert all bounds to
>       integers by int(), and allow as bounds anything that can be so
>       converted instead of just floats.  However, this would change
>       the semantics: 0.3 <= x is not the same as int(0.3) <= x, and it
>       would be confusing for a loop with 0.3 as lower bound to start
>       at zero.  Also, in general int(f) can be very far from f.

...I definitely would expect this to work for all types that know
about comparison and have let's say a successor or predecessor method.
Integers should grow such methods too.  Let's call the successor
method next and confuse the hell out of everyone:)

The loop variable will be of the same type as the left-bound.
For `low <=' it gets as value the value of low, for `low <'
the initialisation value is low.successor.

So different steps are easy, like:

    class Step2(int):
        def successor(self):
            return Step2(self + 2)

    for low < x < 20:

-- 
groetjes, carel



More information about the Python-list mailing list