Integers class...?

Bernhard Herzog bh at intevation.de
Mon Mar 11 13:18:32 EST 2002


Greg Ewing <greg at cosc.canterbury.ac.nz> writes:

> Bernhard Herzog wrote:
> > 
> > Slices support a step parameter, so
> > 
> >     for x in int[9:-1:-1]:
> 
> I don't consider that a good solution, for
> two reasons:
> 
> (1) I'm not sure what the semantics are of
> slices with negative steps.  There are at 
> least 4 equally plausible interpretations of 
> [a:b:-c] if you count all the possible
> combinations of endpoint swapping and 
> open/closed swapping.

I'm not sure about this, too. However, if it meant anything else than in
range it would be very confusing.

> (2) Even if the semantics are well-defined,
> it's too mind-bending remembering and applying
> them correctly in any given situation. It's
> no better than range(a,b,-c) in that respect.

Right. And for the most simple case where you only have the stop value
it seems more inconvenient than range because you have to supply the
start value or assume that int considered as a sequence implicitly
starts at 0 (which would be the only reasonable thing to do, perhaps,
otherwise int[:10] would start with -sys.maxint - 1).

The only advantage I can see in subscripting int over range is that it
could be defined to return an interator. 

It might be better to introduce special code in the interpreter for the
case where the sequence argument of for is just a range call and range
is actually the builtin function. This would only take care of the
optimization possibilities, of course.

   Bernhard

-- 
Intevation GmbH                                 http://intevation.de/
Sketch                                 http://sketch.sourceforge.net/
MapIt!                                           http://www.mapit.de/



More information about the Python-list mailing list