Why Python does *SLICING* the way it does??

Greg Ewing greg at cosc.canterbury.ac.nz
Thu Apr 21 21:29:51 EDT 2005


beliavsky at aol.com wrote:
> I disagree. Programming languages should not needlessly surprise
> people, and a newbie to Python probably expects that x[1:3] =
> [x[1],x[2],x[3]].

But said newbie's expectations will differ considerably
depending on which other language he's coming from. So
he's almost always going to be surprised one way or another.

Python sensibly adopts a convention that long experience
has shown to be practical, rather than trying to imitate
any particular precedent.

> Along the same lines, I think the REQUIREMENT that x[0] rather than
> x[1] be the first element of list x is a mistake. At least the
> programmer should have a choice

Who says the Python programmer doesn't have a choice?

   class NewbieWarmFuzzyList(list):

     def __new__(cls, base, *args):
       obj = list.__new__(cls, *args)
       obj.base = base
       return obj

     def __getitem__(self, i):
       return list.__getitem__(self, i - self.base)

     # etc...

-- 
Greg Ewing, Computer Science Dept,
University of Canterbury,	
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg



More information about the Python-list mailing list