[Tutor] 'slice', etc
Steven D'Aprano
steve at pearwood.info
Sun Dec 8 03:01:33 CET 2013
On Sat, Dec 07, 2013 at 06:43:12AM -0500, eryksun wrote:
> On Sat, Dec 7, 2013 at 5:42 AM, Steven D'Aprano <steve at pearwood.info> wrote:
> > Slices go back to the earliest days of Python, although slice objects
> > may be newer. In the early days, instead of having a single method
> > __getitem__ which sometimes got a slice argument, there was two methods:
> >
> > obj[a] => obj.__getitem__(a)
> > obj[a:b] => obj.__getslice__(a, b)
> > obj[a:b:c] => obj.__getslice__(a, b, c)
>
> There's no such thing as __getslice__(a, b, c).
[...]
My mistake, I had misremembered the details of __getslice__. Thanks for
the correction.
For the record, slice objects existed in Python 1.5, so they have been
around and used for extended (three argument) slicing for a long time.
It's only the two argument slicing that called __getslice__.
[steve at ando ~]$ python1.5
Python 1.5.2 (#1, Aug 27 2012, 09:09:18) [GCC 4.1.2 20080704 (Red Hat
4.1.2-52)] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> class X:
... def __getitem__(self, x):
... return ("getitem", x)
... def __getslice__(self, a, b):
... return ("getslice", a, b)
...
>>> x = X()
>>> x[5]
('getitem', 5)
>>> x[2:8]
('getslice', 2, 8)
>>> x[2::3]
('getitem', slice(2, None, 3))
--
Steven
More information about the Tutor
mailing list