Rationale behind the deprecation of __getslice__?

Terry Reedy tjreedy at udel.edu
Fri Dec 10 02:01:00 CET 2004


"Steven Bethard" <steven.bethard at gmail.com> wrote in message 
news:kl5ud.167249$5K2.75455 at attbi_s03...

> http://docs.python.org/ref/sequence-methods.html
> __getslice__(  self, i, j)
> ...
> Called to implement evaluation of self[i:j].
> ...
> If no __getslice__() is found, a slice object is created instead, and 
> passed to __getitem__() instead.

The  overwhelmingl most common case of a simple slice is more efficiently 
done by having a separate function since no slice object is created.

>>> a=[1,2,3]
>>> def f(): return a[0:1]
...
>>> import dis
>>> dis.dis(f)
          0 SET_LINENO               1

          3 SET_LINENO               1
          6 LOAD_GLOBAL              0 (a)
          9 LOAD_CONST               1 (0)
         12 LOAD_CONST               2 (1)
         15 SLICE+3
         16 RETURN_VALUE

Terry J. Reedy






More information about the Python-list mailing list