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

  • Home
  • SEARCH
  • 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 8055525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:24:57+00:00 2026-06-05T08:24:57+00:00

Is it possible to overload [] (__getitem__) Python operator and chain methods using the

  • 0

Is it possible to overload [] (__getitem__) Python operator and chain methods using the initial memory reference.

Imagine I have a class Math that accepts a list of integer numbers, like this:

class Math(object):
    def __init__(self, *args, **kwargs):
        assert(all([isinstance(item, int) for item in list(args)]))
        self.list = list(args)

    def add_one(self):
        for index in range(len(self.list)):
            self.list[index] += 1

And I want to do something like this:

instance = Math(1,2,3,4,5)
instance[2:4].add_one()

After executing this code instance.list should be [1,2,4,5,5], is this possible?

I know I could do something like add_one(2,4), but this is not the style of API I would like to have if possible.

Thanks

  • 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-05T08:25:01+00:00Added an answer on June 5, 2026 at 8:25 am

    As Winston mentions, you need to implement an auxiliary object:

    class Math(object):
        def __init__(self, *args, **kwargs):
            self.list = list(args)
    
        def __getitem__(self, i):
            return MathSlice(self, i)
    
    class MathSlice(object):
        def __init__(self, math, slice):
            self.math = math
            self.slice = slice
    
        def add_one(self):
            for i in xrange(*self.slice.indices(len(self.math.list))):
                self.math.list[i] += 1
    
    
    instance = Math(1,2,3,4,5)
    instance[2:4].add_one()
    
    print instance.list
    

    How you share the math object with the MathSlice object depends on what you want the semantics to be if the math object changes.

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

Sidebar

Related Questions

Possible Duplicate: Overload ++ operator Say i have a class in which i overloads
I know it's not possible to overload operators in as3, and have been using
Possible Duplicate: Operator Overloading with C# Extension Methods How can i overload those operators,
Is it possible to overload the null-coalescing operator for a class in C#? Say
It's possible to overload the not operator for a class: class TestA { public:
Is it possible to overload the default function operator (the () operator) in C#?
Hey, I'm trying to figure out if it's possible to overload a template class
Possible Duplicate: Java operator overload In c++, we can perform the operator overloading. But
Possible Duplicate: Can I overload an == operator on an Interface? I do understand
Possible Duplicate: How do I overload the square-bracket operator in C#? Basically I want

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.