Why is __getslice__ still implemented?

Discussion in 'Python' started by Torsten Bronger, Apr 10, 2007.

  1. Hallöchen!

    According to <http://docs.python.org/ref/sequence-methods.html>,
    __getslice__ is deprecated. At the moment, I derive an own class
    from unicode and want to implement my own slicing. I found that I
    have to override __getslice__ since __getitem__ isn't called when I
    have something like my_instance[a:b] in my code.

    According to
    <this may
    have efficiency reasons, however, I agree with
    that this
    is quite confusing. It forces people to implement a deprecated
    function after all. I think the docs should say that you still have
    to override __getslice__ when subclassing from a built-in type,
    unless I really don't understand the issue correctly.

    Torsten Bronger, Apr 10, 2007
    1. Advertisements

  2. Torsten Bronger

    James Stroud Guest

    Which version of python are you using?

    chernev 20% /sw/bin/python
    Python 2.5 (r25:51908, Oct 10 2006, 03:45:47)
    [GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    py> class Bob(object):
    .... def __getitem__(self, *args):
    .... print args
    py> b = Bob()
    py> b[4:21:2]
    (slice(4, 21, 2),)
    py> b[5:18:21,2:9:2,8,14:4]
    ((slice(5, 18, 21), slice(2, 9, 2), 8, slice(14, 4, None)),)
    James Stroud, Apr 10, 2007
    1. Advertisements

  3. Hallöchen!

    This should be Bob(unicode).
    Torsten Bronger, Apr 10, 2007
  4. Yes, you do still need to implement __getslice__ if you're subclassing
    a class (like unicode or list) which provides it. The __getslice__
    method can't be removed entirely for backwards compatibility reasons
    (though it is being removed in Python 3000). If you have a specific
    suggestion for what doc should be updated and how, that would be
    helpful. Please post it to:


    (It doesn't need to be a real patch. Plain text is fine as long as you
    indicate where in the documentation it needs to go.)

    Steven Bethard, Apr 10, 2007
  5. Hallöchen!


    Torsten Bronger, Apr 10, 2007
  6. Thanks!

    Steven Bethard, Apr 10, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.