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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:38:29+00:00 2026-06-15T08:38:29+00:00

In Python, is it possible to encapsulate exactly the common slice syntax and pass

  • 0

In Python, is it possible to encapsulate exactly the common slice syntax and pass it around? I know that I can use slice or __slice__ to emulate slicing. But I want to pass the exact same syntax that I would put in the square brackets that would get used with __getitem__.

For example, suppose I wrote a function to return some slice of a list.

def get_important_values(some_list, some_condition, slice):
    elems = filter(some_condition, some_list)
    return elems[slice]

This works fine if I manually pass in a slice object:

In [233]: get_important_values([1,2,3,4], lambda x: (x%2) == 0, slice(0, None))
Out[233]: [2, 4]

But what I want to let the user pass is exactly the same slicing they would have used with __getitem__:

get_important_values([1,2,3,4], lambda x: (x%2) == 0, (0:-1) )

# or

get_important_values([1,2,3,4], lambda x: (x%2) == 0, (0:) )

Obviously this generates a syntax error. But is there any way to make this work, without writing my own mini parser for the x:y:t type slices, and forcing the user to pass them as strings?

Motivation

I could just make this example function return something directly sliceable, such as filter(some_condition, some_list), which will be the whole result as a list. In my actual example, however, the internal function is much more complicated, and if I know the slice that the user wants ahead of time, I can greatly simplify the calculation. But I want the user to not have to do much extra to tell me the slice ahead of time.

  • 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-06-15T08:38:31+00:00Added an answer on June 15, 2026 at 8:38 am

    Perhaps something along the following lines would work for you:

    class SliceMaker(object):
      def __getitem__(self, item):
        return item
    
    make_slice = SliceMaker()
    
    print make_slice[3]
    print make_slice[0:]
    print make_slice[:-1]
    print make_slice[1:10:2,...]
    

    The idea is that you use make_slice[] instead of manually creating instances of slice. By doing this you’ll be able to use the familiar square brackets syntax in all its glory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Python “extend” for a dictionary I know that Python list can be
Possible Duplicate: Python List vs. Array - when to use? I'm working on a
Possible Duplicate: Python, Printing multiple times, I'd like to know how to print a
Is it possible to encapsulate python modules 'mechanize' and 'BeautifulSoup' into a single .py
Possible Duplicate: Python: find first element in a sequence that matches a predicate Is
Possible Duplicate: python dict.add_by_value(dict_2) ? My input is two dictionaries that have string keys
In Python it's possible to create a procedure that has no explicit return. i.e.:
Can someone explain how these results are possible (python 2.6): >>> 1<3>2 True >>>
Possible Duplicate: Python list subtraction operation In Python you can concatenate lists like so:
In python it's possible to use '.' in order to access object's dictionary items.

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.