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 6347879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:18:37+00:00 2026-05-24T21:18:37+00:00

I read something about slice in Python 3. Then I wrote a program, tried

  • 0

I read something about slice in Python 3. Then I wrote a program, tried to implement __getitem__(self, slice(s)). Code goes below:

class NewList:
    def __init__(self, lst):
        print('new list')
        self._list = lst
    def __getitem__(self, x):
        if type(x) is slice:
            return [ self._list[n] for n in range(x.start, x.stop, x.step) ]  #error?
        else:
            return self._list[x]
    ...

nl1 = NewList([1,2,3,4,5])
nl1[1:3]  #error occurs

Then I found out x.step is None, which made range raise an exception.
So, how should I implement the __getitem__ method?

  • 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-24T21:18:39+00:00Added an answer on May 24, 2026 at 9:18 pm

    You need to use the slice.indices method. Given the length of your sequence, it returns a tuple of start, stop, step:

    >>> s = slice(2, 5, None)
    >>> s.indices(10)
    (2, 5, 1)
    
    >>> [x for x in range(*s.indices(10))]
    [2, 3, 4]
    
    >>> s.indices(3)
    (2, 3, 1)
    
    >>> s.indices(0)
    (0, 0, 1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A while ago I've read something about 'inlining'. The .Net compiler will inject code
When I first started reading about and learning ruby, I read something about the
I'm trying to generate POJO's from mapping xml files. I read something about adding
Sometimes I read EAGL, openAL ... wikipedia doesn't tell me something about EAGL.
I was testing something I read earlier about how random Math.random() really is, and
When I try to use curl or file_get_contents to read something like http://example.com/python/json/ from
I read something about this on PHP docs , but it's not clear to
i've read something about Func's and delegates and that they can help you to
On Joel Spolsky's blog , I often read something about Wasabi and if I
I've read something about tail-call optimization in Scheme. But I'm not sure whether I

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.