slice objects vs. []

Hans Nowak wurmy at earthlink.net
Wed May 22 18:15:12 EDT 2002


Fernando Pérez wrote:
> 
> Quinn Dunkan wrote:
> 
> > This seems to imply that [].__getitem__ should accept a slice object.
> > Currently I am writing
> >
> >     def __getitem__(self, k):
> >         if type(k) is type(slice(0)):
> >             return self.data[k.start:k.end]
> >         else:
> >             return self.data[k]
> >
> > ... which is awkward, in addition to losing k.step.
> >
> > BTW, is there any particular reason slice() is not a type?  I expected to be
> > able to write 'isinstance(k, slice)'.
> >
> 
> The docs are a bit outdated, I think. They don't describe __getslice__, which
> is what you want. Her's an example from my code:

[snip]

No, __getslice__ and friends are deprecated:

  http://www.python.org/doc/current/ref/sequence-methods.html#l2h-135

-- 
Hans (base64.decodestring('d3VybXlAZWFydGhsaW5rLm5ldA==')) 
# decode for email address ;-)
The Pythonic Quarter:: http://www.awaretek.com/nowak/



More information about the Python-list mailing list