Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3991474
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:37:13+00:00 2026-05-20T06:37:13+00:00

I have a class that subclasses the list object. Now I need to handle

  • 0

I have a class that subclasses the list object. Now I need to handle slicing. From everything I read on the intertubes this has to be done using the __getitem__ method. At least in Python 2.7+ which is what I’m using. I have done this (see below), but the __getitem__ method isn’t called when I pass in a slice. Instead, a slice is done and a list is returned. I would like a new instance of myList returned.

Please help me discover what is wrong.

Thanks!

class myList(list):

    def __init__(self, items):

        super(myList, self).__init__(items)
        self.name = 'myList'


    def __getitem__(self, index):

        print("__getitem__")
        if isinstance(index, slice):
            print("slice")
            return self.__class__(
                self[x] for x in range(*index.indices(len(self)))
                )
        else: return super(myList, self).__getitem__(index)

if __name__ == "__main__":
    print("\nI'm tesing out custom slicing.\n")

    N = 10
    L = myList(range(N))

    L3 = L[3]
    L02 = L[:2]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T06:37:14+00:00Added an answer on May 20, 2026 at 6:37 am

    See this note:

    object.__getslice__(self, i, j)

    Deprecated since version 2.0: Support
    slice objects as parameters to the
    __getitem__() method. (However, built-in types in CPython currently
    still implement __getslice__().
    Therefore, you have to override it in
    derived classes when implementing
    slicing.

    So, because you subclass list you have to overwrite __getslice__, even though it’s deprecated.

    I think you should generally avoid subclassing builtins, there are too many weird details. If you just want a class that behaves like a list, there is a ABC to help with that:

    from collections import Sequence
    
    class MyList(Sequence):
        def __init__(self, *items):
            self.data = list(items)
    
        def __len__(self):
            return len(self.data)
    
        def __getitem__(self, slice):
            return self.data[slice]
    
    s = MyList(1,2,3)
    # lots of free methods
    print s[1:2], len(s), bool(s), s.count(3), s.index(2), iter(s)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a basic class that derived subclasses inherit from, it carries the basic
I have a class that subclasses NSMutableArray. I init it using: MyClass class =
I have a base class with a virtual method, and multiple subclasses that override
I have a very basic data class that is subclassed from NSObject. I declare
I have class method that returns a list of employees that I can iterate
In C++, I have an Object class, from which a number of other classes
I have list of files stored inside arraylist that I need to download in
I have a List of objects that extend another class: List<? extends Fruit> arguments;
I have multiple classes (B, C and D) that are subclasses of class A.
I have a container class (subclassed from list) where I have overridden the geattr

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.