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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:11:15+00:00 2026-05-30T12:11:15+00:00

I am befuddled over what I think is a very simple and straight forward

  • 0

I am befuddled over what I think is a very simple and straight forward subclass of a list in Python.

Suppose I want all the functionality of list. I want to add several methods to the default set of a list.

The following is an example:

class Mylist(list):

    def cm1(self):
        self[0]=10

    def cm2(self):
        for i,v in enumerate(self):
            self[i]=self[i]+10        

    def cm3(self):
        self=[]         

    def cm4(self):
        self=self[::-1]

    def cm5(self):
        self=[1,2,3,4,5]       

ml=Mylist([1,2,3,4])
ml.append(5)
print "ml, an instance of Mylist: ",ml
ml.cm1()
print "cm1() works: ",ml

ml.cm2()
print "cm2() works: ",ml

ml.cm3() 
print "cm3() does NOT work as expected: ",ml     

ml.cm4()
print "cm4() does NOT work as expected: ",ml     

ml.cm5()
print "cm5() does NOT work as expected: ",ml  

The output:

Mylist:  [1, 2, 3, 4, 5]
cm1() works:  [10, 2, 3, 4, 5]
cm2() works:  [20, 12, 13, 14, 15]
cm3() does NOT work as expected:  [20, 12, 13, 14, 15]
cm4() does NOT work as expected:  [20, 12, 13, 14, 15]
cm5() does NOT work as expected:  [20, 12, 13, 14, 15]

So it seems that a scalar assignment works as I expect and understand. List or slices do not work as I understand. By ‘does not work,’ I mean that the code in the method does not change the instance of ml as the first two methods do.

What do I need to do so that cm3() cm4() and cm5() work?

  • 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-30T12:11:16+00:00Added an answer on May 30, 2026 at 12:11 pm

    The problem here is that in cm3, cm4, and cm5, you are not modifying the object! You are creating a new one in the scope of the member function, and then assigning it to self. The outer scope doesn’t respect this. In cm1 and cm2, you are modifying the same object, so the object stays the same.

    Try using the id function to debug this:

    def cm4(self):
        self=self[::-1]
        print 'DEBUG', id(self)
    
    ...
    
    m1.cm4()
    print 'DEBUG', id(self)
    

    You’ll see that the id is different.

    So, you might ask, well how do I do this? You are lucky that with lists you can assign into a splice. This might not be as easy with other data structures. What this does is keeps the same list, but replaces the items. To do this, do:

    self[:] = ...
    

    So, for example:

    self[:] = self[::-1]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I want to author a generic Max method which returns the maximum of
okay, i'm a bit befuddled with this error. What i'm trying to do here
I am a tad befuddled. I can't reason why the following works: AcceptVerbs(HttpVerbs.Post)] public
I am befuddled why this is allowed public class Foo { class Bar extends
I'm a bit confused because the logical/programmer brain in me says that if all
I'm still befuddled by the NodeList object in Mootools, and I'm hoping someone can
Had a coworker ask me this, and in my brain befuddled state I didn't
I am new to the world of coding as well as PHP and befuddled
Spent all day trying to generate an xmlbeans binding using wsdl2java (Axis2 1.5.4) Completely
Folks, I've been programming high speed software over 20 years and know virtually every

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.